[英]iOS: Positioning navigation bar buttons within custom navigation bar
我正在構建一個帶有自定義導航欄的應用程序。 經過一些研究,我決定使用 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
現在唯一的問題是較大的導航欄意味着導航欄按鈕最終向下太遠,如下所示:
有誰知道我如何糾正按鈕的 position ?
感謝大家的幫助!
更新:
我在視圖 controller 的 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];
您需要將 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];
嘗試在視圖 controller 的viewDidLoad
方法中將按鈕添加到導航欄。
我的解決方案,不是最好的,但對我來說很好。 我的自定義導航欄高度為 55(默認高度為 44)。 我從我的自定義導航欄上剪下只有 44 的高度並將其插入導航欄。 然后我剪切自定義導航欄的下一部分(陰影等)並將其作為圖像視圖插入導航欄下。 就是這樣。 按鈕在正確的位置...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.