开发者

iPhone - How set uinavigationbar height?

开发者 https://www.devze.com 2022-12-17 21:05 出处:网络
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 whi

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];
开发者_JS百科

iPhone - How set uinavigationbar height?


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


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


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.


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)
    }
}


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);
}


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.


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
    }
}


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
    }
}
}


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号