如何设置带边框的UIBarButtonItem

时间:2013-05-07 08:08:06

标签: ios iphone cocoa-touch uibarbuttonitem

我用barbutton创建了视图,如下所示

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

enter image description here

这是这样的 但我希望它为

enter image description here

如何删除按钮的边框?

2 个答案:

答案 0 :(得分:16)

使用此:

UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:customButton];

您可以使用您想要的任何视图初始化按钮。您可以在其中创建一个包含任何(可接受的)视图的按钮。

例如

// GRAPHICAL BUTTON FRAMEWORK
     UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
     [btton setFrame:CGRectMake(0, 0, 30, 30)];
     [btton addTarget:self action:@selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
     [btton setImage:[UIImage imageNamed:@"SOME IMAGE"] forState:UIControlStateNormal];
     UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:btton];

希望它对你有所帮助。

答案 1 :(得分:15)

另一种方法是删除按钮的背景图像:

// portrait
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// landscape
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];