我需要添加一个没有标题和背景图像的按钮,并在点击它时执行操作。我需要在导航栏中添加一个按钮。 我已经制作了这样的导航栏:
-(void)viewDidLoad{
[super viewDidLoad];
[self.navigationBar setFrame:CGRectMake(0,0,320,50)];
self.navigationBar.tintColor=[UIColor whiteColor];
}
我可以看到它的白色导航栏。如何通过图像和动作添加一个按钮?
答案 0 :(得分:1)
使用此代码
-(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
}
答案 1 :(得分:0)
您可以使用以下代码:
UIBarButtonItem *rightBar=[[UIBarButtonItem alloc]initWithImage:@"img.png" style:UIBarButtonItemStyleBordered target:self action:@selector(methodname:)];
答案 2 :(得分:0)
您可以使用自定义视图制作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;