我有一个导航栏(带导航),我在导航栏中包含了2个按钮,但是如果我将其中一个按钮分配到左侧,我的导航按钮会消失(doughh), 那我该怎么办,我的导航按钮和另外两个按钮? 这里是我使用的代码:
来自viewDidLoad的
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
infoButton.frame = CGRectMake(0, 10, 40, 30);
UIImage *img = [UIImage imageNamed:@"eye.png"];
[infoButton setImage:img forState:UIControlStateNormal]; [img release];
//[infoButton setTitle:@"xuxu"forState:UIControlStateNormal];
[infoButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];
[modalButton release];
UIButton* iButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
iButton.frame = CGRectMake(0, 10, 40, 30);
UIImage *imag = [UIImage imageNamed:@"eye.png"];
[iButton setImage:imag forState:UIControlStateNormal]; [imag release];
//[infoButton setTitle:@"xuxu"forState:UIControlStateNormal];
[infoButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton2 = [[UIBarButtonItem alloc] initWithCustomView:iButton];
[self.navigationItem setLeftBarButtonItem:modalButton2 animated:YES];
[modalButton2 release];
所以我的第二个按钮设置为
setLeftBarButtonItem
有没有像setCenter这样的东西?或用x,y ??设定位置的方法 我需要2个按钮,左边的按钮用于导航(3个按钮)
谢谢你!答案 0 :(得分:1)
使用此代码了解如何在导航栏上添加两个或更多按钮
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
infoButton.frame = CGRectMake(0, 10, 40, 30);
UIImage *img = [UIImage imageNamed:@"eye.png"];
[infoButton setImage:img forState:UIControlStateNormal]; [img release];
[infoButton setTitle:@"xuxu"forState:UIControlStateNormal];
[infoButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];
[modalButton release];
UIButton* iButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(0, 10, 40, 30);
iButton.frame = CGRectMake(42, 10, 40, 30);
UIImage *imag = [UIImage imageNamed:@"eye.png"];
[iButton setImage:imag forState:UIControlStateNormal];
[aButton setImage:imag forState:UIControlStateNormal];
[imag release];
[infoButton setTitle:@"xuxu"forState:UIControlStateNormal];
[iButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
[aButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIView *viewForAdd=[[UIView alloc] initWithFrame:CGRectMake(0,0,100,50)];
[viewForAdd addSubview:iButton];
[viewForAdd addSubview:aButton];
UIBarButtonItem *modalButton2 = [[UIBarButtonItem alloc] initWithCustomView:viewForAdd];
[self.navigationItem setLeftBarButtonItem:modalButton2 animated:YES];
[modalButton2 release];