为什么我的UIBarButtonItem没有出现在右上角?

时间:2013-08-13 17:44:26

标签: ios uinavigationbar

我在.m:

中有这个代码的UIViewController
@property (nonatomic,strong) IBOutlet UINavigationBar *navBar;
@property (nonatomic,strong) IBOutlet UIBarButtonItem *button;

@end

@implementation DetailViewController

    - (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIBarButtonItem *listoButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ListoBtn1.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(done:)];

    self.navBar.topItem.rightBarButtonItem = listoButton;

    //self.navBar.topItem.backBarButtonItem = self.button;
    [self setVariables];

}

按钮出现但看起来像这样:

barbuttonitem

我只是希望我的图像出现,而不是后退按钮。如何删除边框?

2 个答案:

答案 0 :(得分:5)

首先,当您在initWithNibName中创建按钮时,NIB尚未完成加载,而您UINavigationBar未自动使用navigationItem(导航控制器自动使用它) )。如果您要将按钮添加到navBar,则应在viewDidLoad

中执行此操作

答案 1 :(得分:1)

要删除边框,请使用UIBarButtonItem customView

创建UIButton
UIButton *listoButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *listoButtonImage = [UIImage imageNamed:@"ListoBtn1.png"];
[listoButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
[listoButton setBackgroundImage:listoButtonImage forState:UIControlStateNormal];
listoButton.frame = CGRectMake(0, 0, listoButtonImage.size.width, listoButtonImage.size.height);
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:listoButton];
self.navBar.topItem.rightBarButtonItem = barButton;