小知識(shí):
1.UIBarButtonItem:描述按鈕具體的內(nèi)容
2.UINavigationItem:設(shè)置導(dǎo)航條上的內(nèi)容(左邊,右邊兆蕉,中間)
3.tabBarItem:設(shè)置tabBar上的按鈕內(nèi)容(tabBarButton)
左右item
問題:
-
UIButton包裝成UIBarButtonItem就會(huì)導(dǎo)致點(diǎn)擊區(qū)域擴(kuò)大
應(yīng)該包裝成UIVIew在進(jìn)行添加
-
擴(kuò)展類UIBarButtonItem-代碼如下
UIBarButtonItem+ZYExtension.h
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (ZYExtension)
+ (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action;
@end
UIBarButtonItem+ZYExtension.m
#import "UIBarButtonItem+ZYExtension.h"
@implementation UIBarButtonItem (ZYExtension)
+ (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:highImage] forSt ate:UIControlStateHighlighted];
[btn sizeToFit];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *containVew = [[UIView alloc] initWithFrame:btn.bounds];
[containVew addSubview:btn];
return [[UIBarButtonItem alloc]initWithCustomView:containVew];
}
使用方法:(例如)
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithNorImage:@"nav_item_game_icon" highImage:@"nav_item_game_click_icon" target:self action:@selector(game)];
設(shè)置導(dǎo)航條標(biāo)題
- 只要通過模型設(shè)置,都需要通過付文本設(shè)置
- 設(shè)置導(dǎo)航條標(biāo)題
self.navigationItem.title = @"我的";
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
[self.navigationController.navigationBar setTitleTextAttributes:attrs]; -
titleVIew
self.navigationItem.titleView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"MainTitle"]];
片段代碼:(調(diào)用)
- (void)setupNavBar
{
>
// 左邊Item
self.navigationItem.leftBarButtonItem = [UIBarButtonItem
itemWithNorImage:@"nav_item_game_icon"
highImage:@"nav_item_game_click_icon"
target:self
action:@selector(game)];
// 右邊Item
self.navigationItem.rightBarButtonItem = [UIBarButtonItem
itemWithNorImage:@"navigationButtonRandom"
highImage:@"navigationButtonRandomClick"
target:self
action:nil];
// 中間titleView
self.navigationItem.titleView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"MainTitle"]];
}
- (void)setupNavBar{
// 設(shè)置
UIBarButtonItem *settingItem = [UIBarButtonItem itemWithNorImage:@"mine-setting-icon" highImage:@"mine-setting-icon-click" target:self action:@selector(setting:)];
// 夜間模式
UIBarButtonItem *nightItem = [UIBarButtonItem itemWithNorImage:@"mine-moon-icon" selImage:@"mine-moon-icon-click" target:self action:@selector(night:)];
>
// 設(shè)置
self.navigationItem.rightBarButtonItems = @[settingItem,nightItem];
// 只要通過模型設(shè)置毡鉴,都需要通過富文本設(shè)置
// 設(shè)置導(dǎo)航條標(biāo)題 ==> UINavigationBar
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
[self.navigationController.navigationBar setTitleTextAttributes:attrs];
}
#pragma mark- 夜間模式以及設(shè)置點(diǎn)擊事件
- (void)night:(UIButton *)btn{
// 選中狀態(tài)-必須手動(dòng)
btn.selected = !btn.selected;
}
- (void)setting:(UIButton *)btn{
}