changing the height of UITabBar in iOS7/8?

前端 未结 8 2044
轮回少年
轮回少年 2020-12-08 05:29

I am trying to change the height of the stock UITabBar to 44px, similar to Tweetbot\'s tab bar height. I\'ve also seen a few other apps do this as well.

相关标签:
8条回答
  • 2020-12-08 05:49

    In your UITabBarController

    - (void)viewWillLayoutSubviews {
        CGRect tabFrame = self.tabBar.frame;
        tabFrame.size.height = 80;
        tabFrame.origin.y = self.view.frame.size.height - 80;
        self.tabBar.frame = tabFrame; 
    }
    
    0 讨论(0)
  • 2020-12-08 05:54

    The developer doesn't own the tabBar, the framework does. It will fight you to make sure that the tabBar stays the same height. If you want to work around this, you can make your own toolbar and add autlayout constraints to its height to force it to stay whatever height you'd like.

    0 讨论(0)
  • 2020-12-08 05:56

    It seems everybody says this can't be done easily

    In your storyboard give your UITabBar a custom subclass name, then implement the subclass with the following

    This tells all views that use the tab bar that it should be a certain height.

    @implementation MyTabBar
    
    -(CGSize)sizeThatFits:(CGSize)size
    {
        CGSize sizeThatFits = [super sizeThatFits:size];
        sizeThatFits.height = 100;
    
        return sizeThatFits;
    }
    
    @end
    

    enter image description here

    0 讨论(0)
  • 2020-12-08 05:56

    In swift it is even simpler than all solutions suggested above by using an extensions to UITabBar, no subclassing necessary:

    extension UITabBar {
    
        override public func sizeThatFits(size: CGSize) -> CGSize {
            super.sizeThatFits(size)
            var sizeThatFits = super.sizeThatFits(size)
            sizeThatFits.height = <Insert your height here>
            return sizeThatFits
        }
    }
    
    0 讨论(0)
  • 2020-12-08 06:00

    For Swift 3 and xcode 8

    extension UITabBar {
         override open func sizeThatFits(_ size: CGSize) -> CGSize {
         var sizeThatFits = super.sizeThatFits(size)
         sizeThatFits.height = 80 // adjust your size here
         return sizeThatFits
        }
     }
    
    0 讨论(0)
  • 2020-12-08 06:03

    SomeGuy's answer above worked for me. Here's the Swift translation for anyone who may need it. I made the height close to what it seems most popular apps use.

    class TabBar: UITabBar {
    
         override func sizeThatFits(size: CGSize) -> CGSize {
             var sizeThatFits = super.sizeThatFits(size)
             sizeThatFits.height = 38
    
             return sizeThatFits
         }
    }
    
    0 讨论(0)
提交回复
热议问题