我收到了以下代码:
- (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的Button alt text http://img693.imageshack.us/img693/1632/bildschirmfoto20100227ui.png
有没有办法在这个位置设置一个普通风格的按钮?
答案 0 :(得分:23)
请改为尝试:
- (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;
}
希望它有所帮助。
答案 1 :(得分:23)
在界面构建器中创建一个UIButton,使其看起来完全符合您的要求。在.h中创建一个插座:
IBOutlet UIButton * _myButton;
然后使用自定义视图将其设置为右侧栏按钮项,这将消除边框:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];
这是有效的,因为UIButton是UIView的子类。
答案 2 :(得分:8)
或者在没有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):
答案 3 :(得分:4)
这很奇怪,但它确实有效。对图像/插座说“不”!您甚至不应该设置UIBarButtonItemStylePlain属性。诀窍是将按钮放入UIToolBar中,并带有一些特殊属性:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Cool plain bar"];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
tools.clipsToBounds = NO;
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTableView)];
[buttons addObject:bi];
[tools setItems:buttons animated:NO];
UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
item.rightBarButtonItem = rightButtons;
item.hidesBackButton = YES;
[myCoolNavigationBar pushNavigationItem:item animated:NO];
答案 4 :(得分:1)
虽然Frank是对的,但这段代码应该在viewDidLoad
中,这里的问题就是BarButtons的样子。如果您希望它看起来不同,则需要使用不同类型的UIView。您可以将UIButton添加到UIToolbar中。
-(void)viewDidLoad {
[super viewDidLoad];
self.title = @"please wait";
self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
}
编辑:
抱歉,你想要一个简单的UIBarButtonItem。我不相信你可以用UINavigationBar做到这一点。你可以尝试添加一个UserInteractionEnabled UILabel作为rightBarButtonItem,我不确定它是否会起作用。
似乎目前无法做到这一点。
sbwoodside has a solution 强>
答案 5 :(得分:0)
为什么不试试UI7Kit?适合我,易于使用。
答案 6 :(得分:-1)
UIImage *img = [UIImage imageNamed:@"white_star.png"];
CGSize itemSize = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:@selector(favoriteButtonClicked)];
barButton.style = UIBarButtonItemStyleBordered;
答案 7 :(得分:-1)
试试这个,
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(didPressButton)];