我正在用自定義導航欄構建應用程序。在做了一些研究後,我決定使用UINavigationBar上的一個類別來做這件事。導航欄需要比平時大一點才能容納投影。下面是代碼:iOS:在自定義導航欄中定位導航欄按鈕
#import "UINavigationBar+CustomWithShadow.h"
@implementation UINavigationBar (CustomWithShadow)
- (void)drawRect:(CGRect)rect {
// Change the tint color in order to change color of buttons
UIColor *color = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:0.0];
self.tintColor = color;
// Add a custom background image to the navigation bar
UIImage *image = [UIImage imageNamed:@"NavBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, 60)];
}
- (void)layoutSubviews {
self.frame = CGRectMake(0, 20, self.frame.size.width, 60);
}
@end
唯一的問題是現在的更大的導航欄是指在導航欄按鈕落得太遠,就像這樣:
有誰知道我可以改正按鈕的位置?
感謝您的幫助!
更新:
我的按鈕添加到視圖控制器的init方法的導航欄,如下所示:
// Create "Add" button for the nav bar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(createNewEntry:)];
[[self navigationItem] setRightBarButtonItem:addButton];
[addButton release];
如何添加'UIBarButtonItem'到酒吧?哪裏?我做了一個小測試,在其中我通過NIB添加了btns,一切似乎都正常。 – 2011-05-29 18:43:54
我將它們添加到視圖控制器的init方法中,例如使用[[self navigationItem] setRightBarButtonItem:addButton];'。我如何通過NIB添加按鈕? – 2011-05-29 19:01:28