2011-04-11 113 views
8

我添加自定義按鈕,導航控制器導航控制器添加自定義按鈕無邊框

 
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] 
                    style:UIBarButtonItemStylePlain 
                    target:self 
                    action:@selector(backAction)]; 
    self.navigationItem.leftBarButtonItem = backButton; 

它工作正常,但按鈕顯示爲界。我該如何解決這個問題?

UPDATE 我找到了解決方案

 
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; 
    [button setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    [button release]; 
self.navigationItem.leftBarButtonItem = back; 

回答

12

試試這個。

UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, button_width, button_height)]; 
[backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; 
相關問題