iPhone - 如何设置 uinavigationbar 高度?

发布于 2024-08-19 06:27:21 字数 449 浏览 4 评论 0原文

我想让导航视图的顶部小一点。你将如何实现这一目标?这是我到目前为止所尝试过的,但正如你所看到的,即使我使导航栏变小,它曾经占据的区域仍然存在(黑色)。

[window addSubview:[navigationController view]];
navigationController.view.frame = CGRectMake(0, 100, 320, 280);
navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 20);
navigationController.view.backgroundColor = [UIColor blackColor];
[window makeKeyAndVisible];

替代文本

I want to make the top of the navigation view a bit smaller. How would you achieve this? This is what I've tried so far, but as you can see, even though I make the navigationbar smaller, the area which it used to occupy is still there (black).

[window addSubview:[navigationController view]];
navigationController.view.frame = CGRectMake(0, 100, 320, 280);
navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 20);
navigationController.view.backgroundColor = [UIColor blackColor];
[window makeKeyAndVisible];

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

小霸王臭丫头 2024-08-26 06:27:21

创建一个具有自定义 sizeThatFits 的 UINavigationBar 类别。

@implementation UINavigationBar (customNav)
  - (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(self.frame.size.width,70);
    return newSize;
  }
@end

Create a UINavigationBar Category with a custom sizeThatFits.

@implementation UINavigationBar (customNav)
  - (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(self.frame.size.width,70);
    return newSize;
  }
@end
堇色安年 2024-08-26 06:27:21

使用这个导航栏子类,我在 iPad 上的 iOS 5.x 到 iOS 6.x 上成功创建了一个更大的导航栏。这给了我一个更大的导航栏,但不会破坏所有动画。

static CGFloat const CustomNavigationBarHeight = 62;
static CGFloat const NavigationBarHeight = 44;
static CGFloat const CustomNavigationBarHeightDelta = CustomNavigationBarHeight - NavigationBarHeight;

@implementation HINavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//      UIColor *titleColor = [[HITheme currentTheme] fontColorForLabelForLocation:HIThemeLabelNavigationTitle];
//      UIFont *titleFont = [[HITheme currentTheme] fontForLabelForLocation:HIThemeLabelNavigationTitle];

//      [self setTitleTextAttributes:@{ UITextAttributeFont : titleFont, UITextAttributeTextColor : titleColor }];

        CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -CustomNavigationBarHeightDelta / 2.0);
        self.transform = translate;
        [self resetBackgroundImageFrame];

    }
    return self;
}

- (void)resetBackgroundImageFrame
{
    for (UIView *view in self.subviews) {
        if ([NSStringFromClass([view class]) rangeOfString:@"BarBackground"].length != 0) {
            view.frame = CGRectMake(0, CustomNavigationBarHeightDelta / 2.0, self.bounds.size.width, self.bounds.size.height);
        }
    }
}

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
{
    [super setBackgroundImage:backgroundImage forBarMetrics:barMetrics];
    [self resetBackgroundImageFrame];
}

- (CGSize)sizeThatFits:(CGSize)size
{
    size.width = self.frame.size.width;
    size.height = CustomNavigationBarHeight;
    return size;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self resetBackgroundImageFrame];
}



@end

Using this navigation bar subclass I've successfully created a larger navigation bar on iOS 5.x to iOS 6.x on the iPad. This gives me a larger navigation bar but doesn't break all the animations.

static CGFloat const CustomNavigationBarHeight = 62;
static CGFloat const NavigationBarHeight = 44;
static CGFloat const CustomNavigationBarHeightDelta = CustomNavigationBarHeight - NavigationBarHeight;

@implementation HINavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//      UIColor *titleColor = [[HITheme currentTheme] fontColorForLabelForLocation:HIThemeLabelNavigationTitle];
//      UIFont *titleFont = [[HITheme currentTheme] fontForLabelForLocation:HIThemeLabelNavigationTitle];

//      [self setTitleTextAttributes:@{ UITextAttributeFont : titleFont, UITextAttributeTextColor : titleColor }];

        CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -CustomNavigationBarHeightDelta / 2.0);
        self.transform = translate;
        [self resetBackgroundImageFrame];

    }
    return self;
}

- (void)resetBackgroundImageFrame
{
    for (UIView *view in self.subviews) {
        if ([NSStringFromClass([view class]) rangeOfString:@"BarBackground"].length != 0) {
            view.frame = CGRectMake(0, CustomNavigationBarHeightDelta / 2.0, self.bounds.size.width, self.bounds.size.height);
        }
    }
}

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
{
    [super setBackgroundImage:backgroundImage forBarMetrics:barMetrics];
    [self resetBackgroundImageFrame];
}

- (CGSize)sizeThatFits:(CGSize)size
{
    size.width = self.frame.size.width;
    size.height = CustomNavigationBarHeight;
    return size;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self resetBackgroundImageFrame];
}



@end
妄司 2024-08-26 06:27:21

为了快速

创建 Uinavigation bar 的子类。

import UIKit

class higherNavBar: UINavigationBar {

override func sizeThatFits(size: CGSize) -> CGSize {
    var newSize:CGSize = CGSizeMake(self.frame.size.width, 87)
    return newSize
}

两侧都会有两个空白条,我将宽度更改为确切的数字以使其正常工作。

然而,标题和后退按钮与底部对齐。

For swift

create a subclass of Uinavigation bar.

import UIKit

class higherNavBar: UINavigationBar {

override func sizeThatFits(size: CGSize) -> CGSize {
    var newSize:CGSize = CGSizeMake(self.frame.size.width, 87)
    return newSize
}

There will be two blank strips on both sides, I changed the width to the exact number to make it work.

However the title and back button are aligned to the bottom.

洛阳烟雨空心柳 2024-08-26 06:27:21

没有必要对 UINavigationBar 进行子类化。在 Objective-C 中,您可以使用类别,在 Swift 中,您可以使用扩展。

extension UINavigationBar {
    public override func sizeThatFits(size: CGSize) -> CGSize {
        return CGSize(width: frame.width, height: 70)
    }
}

It's not necessary to subclass the UINavigationBar. In Objective-C you can use a category and in Swift you can use an extension.

extension UINavigationBar {
    public override func sizeThatFits(size: CGSize) -> CGSize {
        return CGSize(width: frame.width, height: 70)
    }
}
音盲 2024-08-26 06:27:21

我发现以下代码在 iPad(和 iPhone)上表现更好:

- (CGSize)sizeThatFits:(CGSize)size
{
     return CGSizeMake(self.superview.bounds.size.width, 62.0f);
}

I have found the following code to perform better on iPad (and iPhone):

- (CGSize)sizeThatFits:(CGSize)size
{
     return CGSizeMake(self.superview.bounds.size.width, 62.0f);
}
从﹋此江山别 2024-08-26 06:27:21

如果您想为导航栏使用自定义高度,我认为您至少应该使用自定义导航栏(而不是导航控制器中的导航栏)。隐藏 navController 的栏并添加您自己的栏。然后你可以将其高度设置为你想要的任何高度。

If you want to use a custom height for your nav bar, I think you should probably, at the very least, use a custom nav bar (not one in your nav controller). Hide the navController's bar and add your own. Then you can set its height to be whatever you want.

葬花如无物 2024-08-26 06:27:21

我能够在 Swift 中使用以下子类代码。它使用现有高度作为起点并添加到它。

与本页上的其他解决方案不同,在横向和纵向之间切换时,它似乎仍然可以正确调整大小。

class TallBar: UINavigationBar {
    override func sizeThatFits(size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        size.height += 20
        return size
    }
}

I was able to use the following subclass code in Swift. It uses the existing height as a starting point and adds to it.

Unlike the other solutions on this page, it seems to still resize correctly when switching between landscape and portrait orientation.

class TallBar: UINavigationBar {
    override func sizeThatFits(size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        size.height += 20
        return size
    }
}
囍孤女 2024-08-26 06:27:21

这是 Swift 中一个非常好的子类,您可以在 Storyboard 中配置它。它基于 mackross 所做的工作,这很棒,但它是 iOS7 之前的版本,会导致您的导航栏无法延伸到状态栏下方。

class UINaviationBarCustomHeight: UINavigationBar {

// Note: this must be set before the navigation controller is drawn (before sizeThatFits is called),
// so set in IB or viewDidLoad of the navigation controller
@IBInspectable var barHeight: CGFloat = -1
@IBInspectable var barHeightPad: CGFloat = -1

override func sizeThatFits(size: CGSize) -> CGSize {
    var customSize = super.sizeThatFits(size)
    let stockHeight = customSize.height
    if (UIDevice().userInterfaceIdiom == .Pad && barHeightPad > 0) {
        customSize.height = barHeightPad
    }
    else if (barHeight > 0) {
        customSize.height = barHeight
    }
    // re-center everything
    transform = CGAffineTransformMakeTranslation(0, (stockHeight - customSize.height) / 2)
    resetBackgroundImageFrame()
    return customSize
}

override func setBackgroundImage(backgroundImage: UIImage?, forBarPosition barPosition: UIBarPosition, barMetrics: UIBarMetrics) {
    super.setBackgroundImage(backgroundImage, forBarPosition: barPosition, barMetrics: barMetrics)
    resetBackgroundImageFrame()
}

private func resetBackgroundImageFrame() {
    if let bg = valueForKey("backgroundView") as? UIView {
        var frame = bg.frame
        frame.origin.y = -transform.ty
        if (barPosition == .TopAttached) {
            frame.origin.y -= UIApplication.sharedApplication().statusBarFrame.height
        }
        bg.frame = frame
    }
}
}

Here's a pretty nice subclass in Swift that you can configure in Storyboard. It's based on the work done by mackross, which is great, but it was pre-iOS7 and will result in your nav bar not extending under the status bar.

class UINaviationBarCustomHeight: UINavigationBar {

// Note: this must be set before the navigation controller is drawn (before sizeThatFits is called),
// so set in IB or viewDidLoad of the navigation controller
@IBInspectable var barHeight: CGFloat = -1
@IBInspectable var barHeightPad: CGFloat = -1

override func sizeThatFits(size: CGSize) -> CGSize {
    var customSize = super.sizeThatFits(size)
    let stockHeight = customSize.height
    if (UIDevice().userInterfaceIdiom == .Pad && barHeightPad > 0) {
        customSize.height = barHeightPad
    }
    else if (barHeight > 0) {
        customSize.height = barHeight
    }
    // re-center everything
    transform = CGAffineTransformMakeTranslation(0, (stockHeight - customSize.height) / 2)
    resetBackgroundImageFrame()
    return customSize
}

override func setBackgroundImage(backgroundImage: UIImage?, forBarPosition barPosition: UIBarPosition, barMetrics: UIBarMetrics) {
    super.setBackgroundImage(backgroundImage, forBarPosition: barPosition, barMetrics: barMetrics)
    resetBackgroundImageFrame()
}

private func resetBackgroundImageFrame() {
    if let bg = valueForKey("backgroundView") as? UIView {
        var frame = bg.frame
        frame.origin.y = -transform.ty
        if (barPosition == .TopAttached) {
            frame.origin.y -= UIApplication.sharedApplication().statusBarFrame.height
        }
        bg.frame = frame
    }
}
}
深府石板幽径 2024-08-26 06:27:21

我还是ios新手。我通过以下方式解决了问题:

  1. 我创建了一个继承自 UINavigationBar 的新类

  2. 我重写了以下方法:

     (void)setBounds:(CGRect)bounds {
    
       [超级setBounds:边界];
       self.frame = CGRectMake(0, 0, 320, 54);
    
    
      }
    

3.为了获取导航栏的自定义背景,我重写了以下方法:

-(void)drawRect:(CGRect)rect {

    [super drawRect:rect];

    UIImage *img = [UIImage imageNamed:@"header.png"];

    [img drawInRect:CGRectMake(0,0, self.frame.size.width, self.frame.size.height)];


}
  1. 在 xib 文件中,我已将导航栏的默认 UINavigationBar 类更改为我的类。

I am a newbie in ios yet. I solved the problem in following way :

  1. I have created a new class that inherits from UINavigationBar

  2. I override the following method :

     (void)setBounds:(CGRect)bounds {
    
       [super setBounds:bounds];
       self.frame = CGRectMake(0, 0, 320, 54);
    
    
      }
    

3.To get a custom background of the navigation bar, I overrided the following method :

-(void)drawRect:(CGRect)rect {

    [super drawRect:rect];

    UIImage *img = [UIImage imageNamed:@"header.png"];

    [img drawInRect:CGRectMake(0,0, self.frame.size.width, self.frame.size.height)];


}
  1. In xib file, I have changed the default UINavigationBar class of the navigation bar to my class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文