【发布时间】:2011-09-04 09:32:05
【问题描述】:
我正在构建一个带有自定义导航栏的应用。经过一些研究,我决定使用 UINavigationBar 上的一个类别来执行此操作。导航栏需要比平时大一点才能容纳投影。代码如下:
#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,一切似乎都很好。 -
我将它们添加到视图控制器的 init 方法中,例如使用
[[self navigationItem] setRightBarButtonItem:addButton];。如何通过 NIB 添加按钮?
标签: iphone ios uinavigationcontroller uinavigationbar frontend