iOS:在自定义导航栏中定位导航栏按钮

发布于 2024-11-10 12:08:38 字数 1251 浏览 4 评论 0原文

我正在构建一个带有自定义导航栏的应用程序。经过一番研究后,我决定使用 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深巷少女 2024-11-17 12:08:39

您需要添加 leftBarButtonItem 和 rightBarButtonItem 作为自定义项目并弄乱框架......例如:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,5,buttonImage.size.width,buttonImage.size.height)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:delegate action:@selector(barButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:titleString forState:UIControlStateNormal];
[button setTitleColor:CUSTOM_BAR_BUTTON_TITLE_COLOR forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:14]];
[[button titleLabel] setShadowColor:CUSTOM_BAR_BUTTON_SHADOW_COLOR];
[[button titleLabel] setShadowOffset:CGSizeMake(0,-1)];

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];

[[self navigationItem] setRightBarButtonItem:barButton];
[barButton release];

You'll need to add the leftBarButtonItem and rightBarButtonItem as custom items and mess with the frames.... for example:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,5,buttonImage.size.width,buttonImage.size.height)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:delegate action:@selector(barButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:titleString forState:UIControlStateNormal];
[button setTitleColor:CUSTOM_BAR_BUTTON_TITLE_COLOR forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:14]];
[[button titleLabel] setShadowColor:CUSTOM_BAR_BUTTON_SHADOW_COLOR];
[[button titleLabel] setShadowOffset:CGSizeMake(0,-1)];

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];

[[self navigationItem] setRightBarButtonItem:barButton];
[barButton release];
坠似风落 2024-11-17 12:08:39

尝试将按钮添加到视图控制器的 viewDidLoad 方法中的导航栏。

Try adding the buttons to the nav bar in the viewDidLoad method of the view controller instead.

逆光下的微笑 2024-11-17 12:08:39

我的解决方案不是最好的,但对我来说效果很好。我的自定义导航栏的高度为 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文