自定义导航按钮UIBarButtonItem
2017-02-06 17:18
411 查看
自定义导航按钮UIBarButtonItem
时间 2015-08-01 23:15:09刚刚在线
原文 http://www.superqq.com/blog/2015/08/02/custom-navigation-button-uibarbuttonitem/ 主题
iOS开发
基本上每个iOS APP里面都有导航,比如微信、QQ、支付宝。导航可以很方便地帮助我们管理视图控制器(UIViewController)。导航的重要性不言而喻,基本上是每一位iOS初学者都要接触到的问题。
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 };
[/code]
最后别忘了实现
right:方法:
- (void)right:(id)sender { NSLog(@"rightBarButtonItem"); }
自定义文字的UIBarButtonItem
在文章关于导航栏的六个小技巧的第五个技巧里面有自定义
rightBarButtonItem
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,那么想怎么定义都是可以的。
修改导航栏标题的字体
跟iOS 6一样,我们可以使用导航栏的titleTextAttributes属性来定制导航栏的文字风格。在text
attributes字典中使用如下一些key,可以指定字体、文字颜色、文字阴影色以及文字阴影偏移量:
UITextAttributeFont – 字体key
UITextAttributeTextColor – 文字颜色key
UITextAttributeTextShadowColor – 文字阴影色key
UITextAttributeTextShadowOffset – 文字阴影偏移量key
如下代码所示,对导航栏的标题风格做了修改:
1 2 3 4 5 6 7 | NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; shadow.shadowOffset = CGSizeMake(0, 1); [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName, shadow, NSShadowAttributeName, [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]]; |
![](http://beyondvincent.com/images/2013/11/16.jpg)
相关文章推荐
- 详解iOS应用中自定义UIBarButtonItem导航按钮的创建方法
- 自定义导航栏按钮UIBarButtonItem 文字或图片(定制)
- 自定义导航按钮UIBarButtonItem
- iOS开发>学无止境 - 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- iOS之自定义导航按钮UIBarButtonItem的样式
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 自定义导航栏按钮UIBarButtonItem 文字或图片
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航栏返回按钮backBarButtonItem的标题
- 02---按钮的设置 控制器拥有导航栏包装一层导航控制器 添加子控制器 UIBarButtonItem导航按钮 设置导航栏UINavigationBar主题 设置状态栏样式
- UIBarButtonItem-添加自定义Left或者Right按钮
- UIBarButtonItem-添加自定义Left或者Right按钮
- 浅谈 iOS设计之多视图-导航按钮 UIBarButtonItem切换视图的方法