问题描述
- 添加有背景图片的按钮
-
按钮实现有背景图片,没有title,在点击时有相应,将按钮添加到导航条中,导航条如下:-(void)viewDidLoad{ [super viewDidLoad]; [self.navigationBar setFrame:CGRectMake(0,0,320,50)]; self.navigationBar.tintColor=[UIColor whiteColor]; }
现在显示出白色的导航条,接下来怎么添加带图片的按钮?
谢谢
解决方案
-(void)viewDidLoad{
[super viewDidLoad];
[self.navigationBar setFrame:CGRectMake(0,0,320,50)];
self.navigationBar.tintColor=[UIColor whiteColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourImageName"]
style:UIBarButtonItemStyleBordered
target:self action:@selector(yourButtonPressMethodName:)];
self.navigationItem.leftBarButtonItem = item;//use rightBarButtonItem if you want to add button on right dife
}
解决方案二:
用自定义视图设置UIBarButtonItem :
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0, 0.0, 75.0, 40.0)];
[btn setImage:[UIImage imageNamed:@"anyImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = barbutton;
解决方案三:
把系统的导航条隐藏了,自己写一个view吧。系统的可定制性太差了,往往不符合要求。
时间: 2024-11-01 05:45:41