2011-02-25 139 views
0

我有一個導航欄(導航),我已經包括2個按鈕導航欄,但如果我分配一個按鈕左,我的導航按鈕消失(doughh), 所以我怎麼能,我的導航按鈕,還有2個按鈕? 這裏的代碼我使用:iphone 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個按鈕)

謝謝!

+0

上添加兩個或更多的按鈕,你需要三個按鈕2左1右。 – Ishu 2011-02-25 04:15:57

回答

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]; 
+0

非常感謝你,只要我需要,能夠設置x位置,歡呼 – MaKo 2011-02-25 06:03:52

+0

歡迎,歡呼。 – Ishu 2011-02-25 06:26:16