Iphone 在导航栏中添加带有背景图像和操作的按钮
我需要添加一个没有标题和背景图像的按钮,并在单击时执行操作。我需要向导航栏添加一个按钮。 我制作的导航栏如下所示:Iphone 在导航栏中添加带有背景图像和操作的按钮,iphone,xcode,uibutton,uinavigationbar,Iphone,Xcode,Uibutton,Uinavigationbar,我需要添加一个没有标题和背景图像的按钮,并在单击时执行操作。我需要向导航栏添加一个按钮。 我制作的导航栏如下所示: -(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 *rightBar=[[UIBarButtonItem alloc]initWithImage:@"img.png" style:UIBarButtonItemStyleBordered target:self action:@selector(methodname:)];
使用此代码
-(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;
查看此链接可能会对您有所帮助。。。