iPhone - How set uinavigationbar height?

后端 未结 9 2184
情歌与酒
情歌与酒 2020-11-28 05:25

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 navigati

相关标签:
9条回答
  • 2020-11-28 05:59

    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
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:06

    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.

    0 讨论(0)
  • 2020-11-28 06:07

    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)
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:09

    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
    
    0 讨论(0)
  • 2020-11-28 06:15

    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
        }
    }
    }
    
    0 讨论(0)
  • 2020-11-28 06:16

    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 讨论(0)
提交回复
热议问题