iOS:在自定义导航栏中定位导航栏按钮
我正在构建一个带有自定义导航栏的应用程序。经过一番研究后,我决定使用 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];
I'm building an app with a custom navigation bar. After some research I decided to do this using a category on UINavigationBar. The navigation bar needs to be a bit larger than usual to accomodate a drop shadow. Here is the code:
#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
The only problem now is that the larger navigation bar means that the navigation bar buttons end up too far down, like so:
Does anyone know how I can correct the position of the buttons?
Thanks for all help!
Update:
I add the buttons to the nav bar in the init method of the view controller like so:
// Create "Add" button for the nav bar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(createNewEntry:)];
[[self navigationItem] setRightBarButtonItem:addButton];
[addButton release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要添加 leftBarButtonItem 和 rightBarButtonItem 作为自定义项目并弄乱框架......例如:
You'll need to add the leftBarButtonItem and rightBarButtonItem as custom items and mess with the frames.... for example:
尝试将按钮添加到视图控制器的 viewDidLoad 方法中的导航栏。
Try adding the buttons to the nav bar in the
viewDidLoad
method of the view controller instead.我的解决方案不是最好的,但对我来说效果很好。我的自定义导航栏的高度为 55(默认高度为 44)。我从自定义导航栏中剪下仅 44 的高度并将其插入到导航栏。然后,我剪切自定义导航栏的下一部分(阴影等)并将其作为图像视图插入导航栏下方。就是这样。按钮位于正确的位置...
My solution, not the best, but it works for me fine. My custom navigation bar has height 55 (default height is 44). I cut from my custom navigation bar only 44 of height and insert it to the navigation bar. Then I cut next part of my custom navigation bar (shadows etc.) and insert it as image view under the navigation bar. And that's it. Buttons are at right places...