Positioning UITabBar at the top

前端 未结 9 1682
小鲜肉
小鲜肉 2020-11-28 10:30

I\'m a beginner in iOS development. My question is: is it possible to position UITabBar at the top and how? I can\'t position my UITabBar at the top of the view.

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

    If you want something like an UITabBar on top then how about you create a custom UIView and add any number of UIButtons in it. Then link some ViewControllers with each button through Segue. Done!

    You don't need to customise the UITabBar. As aviatorken89 said earlier, it is against the human interface guideline.

    0 讨论(0)
  • 2020-11-28 11:16
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    
        tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
    }
    

    UPDATE IOS 11 ISSUE

    the code above doesnt work on ios 11. so here is workaround that i found.

    override func viewDidLayoutSubviews() {
        tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
    
        super.viewDidLayoutSubviews()
    }
    
    0 讨论(0)
  • 2020-11-28 11:19

    Is it possible? Sure, but it violates the human interface guidelines.

    Screenshots:

    Portrait Landscape Storyboard

    Code:

    TabController.h:

    #import <UIKit/UIKit.h>
    
    @interface TabController : UITabBarController <UITabBarControllerDelegate>
    
    @end
    

    TabController.m:

    #import "TabController.h"
    
    @interface TabController ()
    
    @end
    
    @implementation TabController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.delegate = self;
    }
    
    - (void)viewWillLayoutSubviews
    {
        [super viewWillLayoutSubviews];
    
        [self.tabBar invalidateIntrinsicContentSize];
    
        CGFloat tabSize = 44.0;
    
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
        if (UIInterfaceOrientationIsLandscape(orientation))
        {
            tabSize = 32.0;
        }
    
        CGRect tabFrame = self.tabBar.frame;
    
        tabFrame.size.height = tabSize;
    
        tabFrame.origin.y = self.view.frame.origin.y;
    
        self.tabBar.frame = tabFrame;
    
        // Set the translucent property to NO then back to YES to
        // force the UITabBar to reblur, otherwise part of the
        // new frame will be completely transparent if we rotate
        // from a landscape orientation to a portrait orientation.
    
        self.tabBar.translucent = NO;
        self.tabBar.translucent = YES;
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题