网络小程序开发(小程序开发软件开发)
708
2022-09-02
[IOS]navigation bar自定义按钮
1.建立Category类:
.h:
@interface UIViewController (SMHNavigationBar)- (void)customLeftButton:(NSString*)leftImageName;- (void)onCustomLeftButtonClick:(id)sender;@end
.m:
#define screenWidth [UIScreen mainScreen].bounds.size.width@implementation UIViewController (SMHNavigationBar)- (void)customLeftButton:(NSString*)leftImageName{ UIImage *leftImage = [UIImage imageNamed:leftImageName]; UIButton *navButton = [UIButton buttonWithType:UIButtonTypeCustom]; if (leftImage != nil) { [navButton setImage:leftImage forState:UIControlStateNormal]; } UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 65, 33)]; [navButton addTarget:self action:@selector(onCustomLeftButtonClick:) forControlEvents:UIControlEventTouchUpInside]; if(screenWidth >= 414){ navButton.frame = CGRectMake((screenWidth/61)*3-17, 0, 36, 36); }else{ navButton.frame = CGRectMake((screenWidth/61)*3-14, 0, 36, 36); } [leftView addSubview:navButton]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];}- (void)onCustomLeftButtonClick:(id)sender{ }@end
使用:
[self customLeftButton:@"list2"];
*隐藏回退键:
self.navigationItem.hidesBackButton = YES;
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~