我想自定义图像中的两个按钮添加到导航栏的一些特定位置。
我找到的解决方案,但它是右/左导航栏按钮。
我对那个代码是:
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];
// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
// Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];
我怎样才能做到这一点?
UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[vieww addSubview:yourBtn1];
[vieww addSubview:yourBtn2];
[self.navigationController.navigationBar addSubview:vieww];
如果你想删除yourButtonView然后作出是全球性的对象;
in .h
UIView *vieww;
在.m
-(void)viewWillDisappear:(BOOL)animated
{
[vieww removeFromSuperview];
}
或按照本作更多链接
如果你是使用>的iOS 5,然后用这个。
UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:@" + " style:UIBarButtonItemStyleDone target:self action:@selector(action1:)];
UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:@" - " style:UIBarButtonItemStyleDone target:self action:@selector(action2:) ];
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];
为<iOS 5的u可以使用以下内容:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
tools.barStyle = UIBarStyleBlackOpaque;
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
[buttons addObject:btn1];
[buttons addObject:btn2];
[tools setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
而不是增加工具栏,你可以创建一个UIView的,在该视图中添加两个按钮。
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];
如果你想用一个故事板做这个组合,来看看这个问题 。