将UITabBar定位在顶部

我是iOS开发的初学者。 我的问题是:是否有可能将UITabBar定位在顶端? 我无法将我的UITabBar放在视图的顶部。

可能吗? 当然,但是这违反了人机界面指南。

截图:

肖像景观故事板

码:

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 

Swift3:我通过为UITabBarController创build一个自定义类来达到这个目的:

 class CustomTabBarController: UITabBarController { @IBOutlet weak var financialTabBar: UITabBar! override func viewDidLoad() { super.viewDidLoad() // I've added this line to viewDidLoad UIApplication.shared.statusBarFrame.size.height financialTabBar.frame = CGRect(x: 0, y: financialTabBar.frame.size.height, width: financialTabBar.frame.size.width, height: financialTabBar.frame.size.height) } 

不要忘记将您的自定义类设置为TabBarController 在这里输入图像说明

结果会是这样的:

在这里输入图像说明

这是一个工作迅速3 aviatorken89的代码的例子。

  1. 首先创build一个新文件。
  2. select源cocoa触摸类。
  3. 指定UITabBarController的子类
  4. 命名类“CustomTabBarController”或任何你想要的。
  5. 将下面的代码添加到类中。

     override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() var tabFrame:CGRect = self.tabBar.frame tabFrame.origin.y = self.view.frame.origin.y self.tabBar.frame = tabFrame } 
  6. 如果您正在使用故事板,请确保通过“身份检查器”将标签栏类更改为您的自定义类。

你可以通过自己创build一个自定义的标签栏,但是苹果极力阻止模仿一个它原本不打算做的function的系统控制。

再一次,我劝阻你这样做,因为它违反了你的应用程序和系统应用程序的一致性。 但是因为我在这里,所以我们去:

对于自定义选项卡栏,您需要创build一个包含多个button的视图。 您还需要在UITabBar 下面的容器视图(因为您希望UITabBar位于顶部)。 当按下button时,您将更改容器内的UIViewController
它很简单,但当然,强烈不build议。

如果你想要一个UITabBar在上面那么你怎么样创build一个自定义的UIView并添加任何数量的UIButtons在它。 然后通过塞格链接一些ViewControllers与每个button。 完成!

你不需要自定义UITabBar。 正如Aviatorken89早些时候所说,这是违反人机界面的准则。