今天在做項(xiàng)目的時(shí)候發(fā)現(xiàn)一個(gè)問(wèn)題,在設(shè)置導(dǎo)航欄左邊按鈕(導(dǎo)航欄上面的東西由棧頂控制器決定)的時(shí)候,如果是用UIButton包裝成UIBarButtonItem設(shè)置為導(dǎo)航條左邊的按鈕,那么會(huì)導(dǎo)致按鈕可點(diǎn)擊范圍增大,這樣的用戶體驗(yàn)是非常不好的,下面是出問(wèn)題的源碼:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 設(shè)置導(dǎo)航條按鈕
[self setUpNavBar];
}
- (void)setUpNavBar
{
UIButton *btn = [[UIButton alloc] init];
[btn setImage:[UIImage imageNamed:@"nav_item_game_icon"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"nav_item_game_click_icon"] forState:UIControlStateHighlighted];
[btn sizeToFit];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = item;
}
- 之后我采用UIView包裝UIButton在把UIView包裝成UIBarButtonItem設(shè)置為導(dǎo)航條左邊的按鈕,點(diǎn)擊范圍就正常了
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 設(shè)置導(dǎo)航條按鈕
[self setUpNavBar];
}
- (void)setUpNavBar
{
UIButton *btn = [[UIButton alloc] init];
[btn setImage:[UIImage imageNamed:@"nav_item_game_icon"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"nav_item_game_click_icon"] forState:UIControlStateHighlighted];
[btn sizeToFit];
// 添加的代碼
UIView *itemView = [[UIView alloc] initWithFrame:btn.bounds];
[itemView addSubview:btn];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:itemView];
self.navigationItem.leftBarButtonItem = item;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者