Change iPhone navigation bar's height

前端 未结 6 367
栀梦
栀梦 2020-12-01 09:56

My client can\'t read iPhone\'s default fonts, the size is too small. I have an application with a navigation bar and I need to make everything in it bigger, for example, th

6条回答
  •  误落风尘
    2020-12-01 10:30

    By subclassing you can achieve that and still support iOS 3+:

    Complete example:

    #import 
    
    @interface ASNavigationBar : UINavigationBar
    @property (nonatomic , retain) UIImage *backgroundImage;
    @end
    

    And implementation:

    #import "ASNavigationBar.h"
    
    @implementation ASNavigationBar
    @synthesize backgroundImage = _backgroundImage;
    
    -(void) setBackgroundImage:(UIImage *)backgroundImage
    {
        if (_backgroundImage != backgroundImage)
        {
            [_backgroundImage release];
            _backgroundImage = [backgroundImage retain];
            [self setNeedsDisplay];
        }
    }
    
    -(void) drawRect:(CGRect)rect
    {
        // This is how the custom BG image is actually drawn
        [self.backgroundImage drawInRect:rect];
    }
    
    - (CGSize)sizeThatFits:(CGSize)size 
    {
        // This is how you set the custom size of your UINavigationBar
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height);
        return newSize;
    }
    @end
    

    Important notes:

    1. If the background image is with transparent areas, you have to set its barStyle property to "translucent" or the transparent areas will be black.
    2. If you have a NavigationBar taller than 44 points, you have to take into account that the position of the BarButtonItems might not be correct. They all will be anchored to the bottom of the bar. you can fix that by overriding layoutSubviews and change their origin.y value.

提交回复
热议问题