我在.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];
}
按钮出现但看起来像这样:
我只是希望我的图像出现,而不是后退按钮。如何删除边框?
答案 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;