[英]UINavigationBar - Change Height / Add Big Button
I need to change the height of my Navigation Bar and add a custom Image button to the top left corner. 我需要更改导航栏的高度并将自定义图像按钮添加到左上角。 I am part way there, but lost now on getting the custom Image button in the right position. 我在那儿,但由于将自定义图像按钮放在正确的位置而迷失了方向。 Here is what I have: 这是我所拥有的:
To adjust the height I have created a UINavBar category with one method as follows: @implementation UINavigationBar (myNavBar) 为了调整高度,我使用以下一种方法创建了UINavBar类别:@implementation UINavigationBar(myNavBar)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(768,80);
return newSize;
}
@end
I have also created a UINavigationController subclass to modify the button. 我还创建了一个UINavigationController子类来修改按钮。 Here is the viewDidLoad from that class: 这是该类的viewDidLoad:
UIImage *navBackgroundImage = [UIImage imageNamed:@"bar"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
// Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:@"back_off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// Change the appearance of other navigation button
UIImage *barButtonImage = [[UIImage imageNamed:@"menu_off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
So far this solution resizes the top nav bar, but positions my button in a weird position. 到目前为止,此解决方案调整了顶部导航栏的大小,但将我的按钮放置在一个奇怪的位置。 Here are what I want vs. what is happening: 这是我想要的与正在发生的事情:
I have a UIBarButtonItem Category that I use which has an offset property: 我有一个UIBarButtonItem类别,该类别具有offset属性:
@interface UIBarButtonItem (CustomImage)
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action offset:(CGPoint)offset;
@end
#import "UIBarButtonItem+CustomImage.h"
@implementation UIBarButtonItem (CustomImage)
+ (UIBarButtonItem *)barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action offset:(CGPoint)offset {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBounds:CGRectOffset(button.bounds, 0.0, -10.0)];
UIView *container = [[UIView alloc] initWithFrame:button.frame];
[container setBounds:CGRectOffset(container.bounds, offset.x, offset.y)];
[container addSubview:button];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];
return item;
}
@end
#import "UIBarButtonItem+CustomImage.h"
UIBarButtonItem *settingsButton = [UIBarButtonItem barItemWithImage:settingsImage
target:self
action:@selector(revealSettings:)
offset:CGPointMake(0.0, 0.0)];
[self.navigationItem setLeftBarButtonItem:settingsButton];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.