详解iOS应用中自定义UIBarButtonItem导航按钮的创建方法
iOS系统导航栏中有leftBarButtonItem和rightBarButtonItem,我们可以根据自己的需求来自定义这两个UIBarButtonItem。
四种创建方法
系统提供了四种创建的方法:
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
- (instancetype)initWithCustomView:(UIView *)customView;
通过系统UIBarButtonSystemItem创建
自定义rightBarButtonItem,代码如下:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];
UIBarButtonSystemItem有以下样式可以供选择:
复制代码 代码如下:
typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
#if __IPHONE_3_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
UIBarButtonSystemItemUndo,
UIBarButtonSystemItemRedo,
#endif
#if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
UIBarButtonSystemItemPageCurl,
#endif
};
最后别忘了实现right:方法:
复制代码 代码如下:
- (void)right:(id)sender
{
NSLog(@"rightBarButtonItem");
}
自定义文字的UIBarButtonItem
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
UIBarButtonItemStyle有以下三种选择:
typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {
UIBarButtonItemStylePlain,
UIBarButtonItemStyleBordered NS_ENUM_DEPRECATED_IOS(2_0, 8_0, "Use UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later"),
UIBarButtonItemStyleDone,
};
实现back:方法:
复制代码 代码如下:
- (void)back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
自定义照片的UIBarButtonItem
复制代码 代码如下:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"test"] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
自定义UIView的UIBarButtonItem
自定义UIView,然后通过initWithCustomView:方法来创建UIBarButtonItem。
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 60)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];
看到有朋友在后台提问:
我现在即需要改那个导航原生的返回图片,也要改返回文字,应该怎么改呢,求指教。
其实,这个就可以用initWithCustomView:来解决,自定义UIView你可以放UIImageView和UILabel。可以自定义UIView,那么想怎么定义都是可以的。
下面来看一个有趣的例子:
先说一下需求:
1.做一个RightBarButtonItem不断旋转的Demo;
2.点击RightBarButtonItem 按钮旋转或暂停;
最终效果展示:
就是那个音符图形的旋转。
关键代码展示(已加注释):
//
// ViewController.m
// NavigationBtn
//
#import "ViewController.h"
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
///ImageView旋转状态枚举
typedef enum {
RotateStateStop,
RotateStateRunning,
}RotateState;
@interface ViewController ()
{
///旋转角度
CGFloat imageviewAngle;
///旋转ImageView
UIImageView *imageView;
///旋转状态
RotateState rotateState;
}
@end
复制代码 代码如下:
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"微信公众账号:乐Coding";
[self buildBarButtonItem];
}
#pragma mark 添加 RightBarButtonItem
-(void)buildBarButtonItem{
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
imageView.autoresizingMask = UIViewAutoresizingNone;
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.bounds=CGRectMake(0, 0, 40, 40);
//设置视图为圆形
imageView.layer.masksToBounds=YES;
imageView.layer.cornerRadius=20.f;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 40);
[button addSubview:imageView];
[button addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpInside];
imageView.center = button.center;
//设置RightBarButtonItem
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barItem;
}
#pragma mark 点击 RightBarButtonItem
- (void)animate {
//改变ImageView旋转状态
if (rotateState==RotateStateStop) {
rotateState=RotateStateRunning;
[self rotateAnimate];
}else{
rotateState=RotateStateStop;
}
}
#pragma mark 旋转动画
-(void)rotateAnimate{
imageviewAngle+=50;
//0.5秒旋转50度
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(imageviewAngle));
} completion:^(BOOL finished) {
if (rotateState==RotateStateRunning) {
[self rotateAnimate];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
您可能感兴趣的文章:
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- oracle em 按钮乱码解决办法及em网页变成英文
- css文本框与按钮美化效果代码
- 用CSS实现的一张图完成的按钮效果
- css样式之区分input是按钮还是文本框的方法第1/4页
- jQuery实现下滑菜单导航效果代码
- jquery实现的仿天猫侧导航tab切换效果
- Android实现定制返回按钮动画效果的方法
- jQuery移动页面开发中主题按钮的设计示例
- ON_COMMAND_RANGE多个按钮响应一个函数的解决方法
- PHP的栏目导航程序
- jquery实现向下滑出的二级导航下滑菜单效果
- 基于jQuery实现以手风琴方式展开和折叠导航菜单
- C#利用反射技术实现去掉按钮选中时的边框效果