码迷,mamicode.com
首页 > 其他好文 > 详细

UIBarButtonItem

时间:2015-02-28 15:58:52      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

 1 增加导航栏按钮方式一
UIBarButtonItem的分类中增加下面的方法
2 +(UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName target:(id)target action:(SEL)action; 3 { 4 UIButton *button = [[UIButton alloc]init]; 5 //设置背景图片 6 [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 7 [button setBackgroundImage:[UIImage imageNamed:highImageName] forState:UIControlStateHighlighted]; 8 //按钮大小设置 9 button.size = button.currentBackgroundImage.size; 10 //添加监听事件 11 [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 12 return [[UIBarButtonItem alloc]initWithCustomView:button]; 13 } 14 15 self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"imageName” highImageName:@“highImageName” target:self action:@selector(friendSearch)]; 16 17 self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"imageName" highImageName:@"highImageName" target:self action:@selector(pop)]; 18 19 增加导航栏按钮方式二 20 第一种方法:直接在控制器中定义UIBarButtonItem的样式并添加按钮 21 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 22 [button setTitle:@"写私信" forState:UIControlStateNormal]; 23 [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; 24 [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; 25 [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled]; 26 button.titleLabel.font = [UIFont systemFontOfSize:15]; 27 button.size = [button.currentTitle sizeWithFont:button.titleLabel.font]; 28 29 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button]; 30 31 第二种方法:在导航控制器中设置UIBarButtonItem的全局样式,在各个控制器中添加按钮 32 //1.在导航控制器中设置UIBarButtonItem的样式,全局有效 33 +(void)initialize //该方法只会调用一次 34 { 35 //通过appearance对象能修改整个项目中所有UIBarButtonItem的样式 36 UIBarButtonItem *appearance = [UIBarButtonItem appearance]; 37 38 //1.普通状态 39 NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary]; 40 textAttrs[UITextAttributeTextColor] = [UIColor orangeColor]; 41 textAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:15]; 42 [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; 43 44 //2.高亮状态 45 NSMutableDictionary *highAttrs = [NSMutableDictionary dictionary]; 46 highAttrs[UITextAttributeTextColor] = [UIColor redColor]; 47 highAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:15]; 48 [appearance setTitleTextAttributes:highAttrs forState:UIControlStateHighlighted]; 49 50 //3.不可点击状态 51 NSMutableDictionary *disAttrs = [NSMutableDictionary dictionary]; 52 disAttrs[UITextAttributeTextColor] = [UIColor lightGrayColor]; 53 disAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:15]; 54 [appearance setTitleTextAttributes:disAttrs forState:UIControlStateDisabled]; 55 56 } 57 //2.在控制器中添加导航栏按钮 58 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"写私信" style:UIBarButtonItemStyleDone target:self action:@selector(composeMsg)];

 

UIBarButtonItem

标签:

原文地址:http://www.cnblogs.com/jsnan/p/4305388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!