Change the height of NavigationBar and UIBarButtonItem elements inside it in Cocoa Touch

后端 未结 6 2067
忘了有多久
忘了有多久 2020-12-23 15:19

I suppose it\'s not strictly in line with Apple guidelines but I guess it must be possible somehow. I\'d like to change the height of navigation bar inside UINavigationContr

6条回答
  •  春和景丽
    2020-12-23 15:30

    How badly do you want this? And, how thin (or thick) do you want to make your navbar?

    One approach would be to set the transform of the navbar to scale and translate it. If you scale it too much the title and button text will look wonky, but if you only need to shave a few pixels you might be allright.

    Here's the result of scaling it to be 75% of full height (33 pixels tall):

    enter image description here

    And the code that produced this:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.navigationItem.title = @"Thin Navigation Bar";
    
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"Press Me" style:UIBarButtonItemStyleBordered target: nil action: NULL ] autorelease];
    
        CGFloat scale = .75;
        CGFloat cy = 44.0 - ( 44.0 * scale );
    
        self.navigationController.navigationBar.transform = CGAffineTransformScale( CGAffineTransformMakeTranslation( 0, -cy / 2.0 ), 1.0, scale )  ;
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        CGFloat scale = .75;
        CGFloat cy = 44.0 - ( 44.0 * scale );
    
        CGRect r = self.view.frame;
        r.origin.y -= cy;
        r.size.height += cy;
        self.view.frame = r;
    }
    

    Now, this does have a number of problems, which may or may not be solvable. #1 is that you're fighting with the UINavigationController to size and position the navbar and the view-controller views. Animating between view controllers that use this technique is likely going to look weird.

    I'd be curious if you could solve the related issues...

    One last thought: If you dont use a UINavigationController then there really aren't a whole lot of issues with this other than squished text. Or, you could use a navigation controller but hide the default navbar, and add the thin navbar to each of your child-view controller views. You could even subclass UINavigationBar and set the transform from within:

    @interface TSThinNavBar : UINavigationBar
    {
    }
    @end
    
    @implementation TSThinNavBar
    
    // assuming we'll always be constructed from a nib
    - (id) initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder: aDecoder];
        if ( self != nil ) 
        {
            CGFloat scale = .75;
            CGFloat cy = 44.0 - ( 44.0 * scale );
    
            self.transform = CGAffineTransformScale( CGAffineTransformMakeTranslation( 0, -cy / 2.0 ), 1.0, scale )  ;
        }
    
        return self;
    }
    
    @end
    

提交回复
热议问题