gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:07:12 31 4
gpt4 key购买 nike

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

现在唯一的问题是较大的导航栏意味着导航栏按钮太低了,如下所示:

enter image description here

有谁知道如何修正按钮的位置?

感谢大家的帮助!

更新:

我在 View 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];

关于iphone - iOS:在自定义导航栏中定位导航栏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6169474/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com