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

后端 未结 6 2076
忘了有多久
忘了有多久 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:28

    This is my solution. It works very well.

    @interface UINavigationBar (CustomHeight)
    
    @end
    
    @implementation UINavigationBar (CustomHeight)
    
    - (CGSize)sizeThatFits:(CGSize)size {
        // Change navigation bar height. The height must be even, otherwise there will be a white line above the navigation bar.
        CGSize newSize = CGSizeMake(self.frame.size.width, 40);
        return newSize;
    }
    
    -(void)layoutSubviews {
        [super layoutSubviews];
    
        // Make items on navigation bar vertically centered.
        int i = 0;
        for (UIView *view in self.subviews) {
            NSLog(@"%i. %@", i, [view description]);
            i++;
            if (i == 0)
                continue;
            float centerY = self.bounds.size.height / 2.0f;
            CGPoint center = view.center;
            center.y = centerY;
            view.center = center;
        }
    }
    

提交回复
热议问题