简体   繁体   English

如何在iOS 8的UINavigationViewController中更改UINavigationBar的大小?

[英]How can I change size of UINavigationBar in UINavigationViewController in iOS 8?

What is the proper way to change height of navigation bar? 更改导航栏高度的正确方法是什么?

I need to create custom title in navigation bar, it should contains two UILabels one above other. 我需要在导航栏中创建自定义标题,它应该包含两个UILabel,一个位于另一个之上。 The Title should be resized to fit those UILabels. 应该调整标题的大小以适合那些UILabel。

Should I override sizeThatFits : method in my custom TitleView, would other buttons correctly change to fit that size? 我应该在自定义TitleView中重写sizeThatFits :方法,其他按钮是否会正确更改以适合该大小? How can I change a size of NavigationBar? 如何更改NavigationBar的大小?

That is what I need to create using latest SDK features. 这就是我需要使用最新的SDK功能创建的内容。

在此处输入图片说明

Create the following class category (you can create it in your implementation file): 创建以下类类别(您可以在实现文件中创建它):

#import "objc/runtime.h"

@interface UINavigationBar (CustomHeight)

- (void)setHeight:(CGFloat)height;

@end

static char const *const kHeight = "Height";

@implementation UINavigationBar (CustomHeight)

- (void)setHeight:(CGFloat)height
{
    objc_setAssociatedObject(self, kHeight, @(height), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)height
{
    return objc_getAssociatedObject(self, kHeight);
}

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize newSize;

    if (self.height) {
        newSize = CGSizeMake(self.superview.bounds.size.width, [self.height floatValue]);
    } else {
        newSize = [super sizeThatFits:size];
    }

    return newSize;
}
@end

And after that, simply call [self.navigationController.navigationBar setHeight:100.0] in your viewDidLoad or where you need it. 然后,只需在您的viewDidLoad或您需要的地方调用[self.navigationController.navigationBar setHeight:100.0] Works in both iOS 7.1 and 8.1. 在iOS 7.1和8.1中均可使用。

Disclaimer: Any alteration of the API and its functions is prone to future issues with new OS releases! 免责声明:对API及其功能的任何更改都容易在新OS版本中出现将来的问题! Apple does not intend to allow us to change the navigation bar height (except for some rare instances) so use any solution wisely after assessing the risks vs advantages. 苹果公司无意允许我们更改导航栏的高度(少数情况除外),因此在评估风险与优势之后,请明智地使用任何解决方案。

Add custom view for both navigation title and navigation right bar button. 为导航标题和导航右栏按钮添加自定义视图。

Here how you can add title view like above. 在这里,您可以像上面一样添加标题视图。

// Custom view for navigation title.
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 22)];
label1.text = @"Stasik";
label1.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
label1.textAlignment = NSTextAlignmentCenter;
[customTitleView addSubview:label1];

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, 100, 22)];
label2.text = @"Stasik iPhone";
label2.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:16];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor darkGrayColor];
[customTitleView addSubview:label2];

self.navigationItem.titleView = customTitleView;

// Custom view for right navigation.

UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
upButton.frame = CGRectMake(0, 0, 20, 44);
[upButton setImage:[UIImage imageNamed:@"up"] forState:UIControlStateNormal];
[upButton addTarget:self action:@selector(upButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *upButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:upButton];

UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
downButton.frame = CGRectMake(0, 0, 20, 44);
[downButton setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
[downButton addTarget:self action:@selector(downButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *downButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:downButton];

// Remove trailing space for right view.
UIBarButtonItem *nagativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
nagativeSpace.width = -11;

self.navigationItem.rightBarButtonItems = @[nagativeSpace, upButtonBarItem, downButtonBarItem];

// Left item
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 40, 44);
[backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

// Remove leading space for left view.
nagativeSpace.width = -15;

UIBarButtonItem *backButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItems = @[nagativeSpace, backButtonBarItem];

OutPut: 输出:

在此处输入图片说明

Arrow images are different than OP's requirement but it serves layout for navigation views. 箭头图像不同于OP的要求,但它可用于导航视图的布局。

From above code make change in nagativeSpace.width value to arrange buttons with accurate distance from left as well as right margins. 从上面的代码中,更改nagativeSpace.width值,以安排距左右边缘的准确距离的按钮。

Looking at that design that you posted, I don't have the impression that the bar is really higher than the standard 44pt. 看看您发布的设计,我没有感觉到该条确实比标准44pt高。 Please note that starting with iOS7 the status bar is integrated into the same bar and the 20pt of the status bar is added to the total height. 请注意,从iOS7开始,状态栏集成在同一栏中,状态栏的20pt被添加到总高度中。

IMHO, the only problem you need to solve is to stack the title/subtitle, and that can be easily done with a custom titleView , as Kampai has demonstrated. 恕我直言,您需要解决的唯一问题是堆叠标题/字幕,这可以通过自定义titleView轻松完成,如Kampai所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改ABPeoplePickerNavigationController的UINavigationBar的颜色? - How can I change color of UINavigationBar of ABPeoplePickerNavigationController? 如何在iOS 7中将UINavigationBar的高度从64点更改为44点? - How can I change the height of a UINavigationBar height from 64 to 44 points in iOS 7? 如何更改UINavigationBar标题标签大小? - How do I change UINavigationBar title label size? 如何在iOS 7上的UINavigationBar中更改文本的颜色? - How do I change the color of my text in my UINavigationBar on iOS 7? 如何在iOS7上使UINavigationBar完全透明? - How can I make my UINavigationBar totally transparent on iOS7? 如何在ios5中更改UINavigationbar的高度 - How to change height of UINavigationbar in ios5 如何在UINavigationController中更改不同的UINavigationBar样式? - How can I change different UINavigationBar Style in a UINavigationController? 如何更改每个视图控制器的UINavigationBar - How can I change UINavigationBar per view controller 我怎么能动画UINavigationBar的条形色调变化,而不是UITabBar? - How is it I can animate the change in bar tint color of a UINavigationBar but not a UITabBar? 如何在UIViewController push上为UINavigationBar背景更改设置动画? - how can I animate the UINavigationBar background change on UIViewController push?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM