0

我有一个导航栏(带导航),我在导航栏上包含了 2 个按钮,但是如果我将其中一个按钮分配到左侧,我的导航按钮就会消失(面团),所以我怎么能拥有我的导航按钮和还有2个按钮?这里是我使用的代码:

来自 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 个按钮)

谢谢你!

4

1 回答 1

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];
于 2011-02-25T05:12:48.767 回答