【问题标题】:iOS: Positioning navigation bar buttons within custom navigation bariOS:在自定义导航栏中定位导航栏按钮
【发布时间】: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


【解决方案1】:

您需要将 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];

【讨论】:

  • 有更简单的方法吗?我在后续视图上有按钮,如果可能的话,我希望避免将它们中的每一个都添加为自定义项。如果您有任何建议,请随意建议我将阴影添加到导航栏的方式的替代方法:)
  • @Erik 您将不得不使用 customView-bar-button-item 将实际按钮作为子视图来解决此类偏移问题。
  • 在所有导航按钮上实现此目的的更简单方法是创建 UINavigationBarButton 的子类或类别。
  • 这听起来是个好主意,亚当!你知道我需要重写哪些方法以及如何重写,或者你知道我会去哪里找到这些信息?
【解决方案2】:

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

【讨论】:

  • 恐怕没有变化。按钮继续与导航栏的底部对齐(向下到投影的末尾)。
【解决方案3】:

我的解决方案,不是最好的,但对我来说很好。我的自定义导航栏高度为 55(默认高度为 44)。我从我的自定义导航栏上剪下只有 44 的高度并将其插入导航栏。然后我剪切自定义导航栏的下一部分(阴影等)并将其作为图像视图插入导航栏下。就是这样。按钮在正确的位置...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多