我得到以下代码:
- (id)init { if (self = [super init]) { self.title = @"please wait"; UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"star.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFavoriteClicked:)]; self.navigationItem.rightBarButtonItem = favorite; } return self; }
但我的按钮看起来仍像一个带有UIBarButtonItemStyleBordered替换文字的按钮 http://img693.imageshack.us/img693/1632/bildschirmfoto20100227ui.png
有没有办法在这个位置设置一个朴素的按钮?
试试这个:
- (id)init { if (self = [super init]) { self.title = @"please wait"; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; [button setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button]; [button release]; self.navigationItem.rightBarButtonItem = favorite; } return self; }
希望能帮助到你.
在界面构建器中创建一个UIButton,使其看起来完全符合您的要求.在.h中创建一个插座:
IBOutlet UIButton * _myButton;
然后使用自定义视图将其设置为右侧栏按钮项,这将消除边框:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];
这是有效的,因为UIButton是UIView的子类.
或者在没有IB的代码中执行此操作:
// add setting button to nav bar UIImage* settingsGearImg = [UIImage imageNamed:@"settings_gear.png"]; CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height); UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg]; [settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal]; [settingsButton addTarget:self action:@selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside]; [settingsButton setShowsTouchWhenHighlighted:YES]; UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton]; self.navigationItem.leftBarButtonItem = settingButtonItem;
结果是:
使用此图标图像时(白色背景上为白色,因此在此处不可见 - 链接为:http://i.stack.imgur.com/lk5SB.png):