ios 将 UITabBar 定位在顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29579992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:35:01  来源:igfitidea点击:

Positioning UITabBar at the top

iosobjective-cswiftuitabbar

提问by AlgroDev

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.

我是 iOS 开发的初学者。我的问题是:是否可以将 UITabBar 放置在顶部以及如何放置?我无法将我的 UITabBar 定位在视图的顶部。

回答by klcjr89

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

是否可以?当然可以,但它违反了人机界面指南。

Screenshots:

截图:

PortraitLandscapeStoryboard

肖像风景故事板

Code:

代码:

TabController.h:

TabController.h:

#import <UIKit/UIKit.h>

@interface TabController : UITabBarController <UITabBarControllerDelegate>

@end

TabController.m:

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

回答by Milad Faridnia

Swift3:I achieve this by creating a custom class for UITabBarController:

Swift3:我通过为以下对象创建自定义类来实现这一点UITabBarController

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)
    }

Don't forget to set your Custom Class to TabBarControllerenter image description here

不要忘记将您的自定义类设置为 TabBarController在此处输入图片说明

The result would be like this:

结果是这样的:

enter image description here

在此处输入图片说明

回答by Leo

Here is a working swift 3 example of aviatorken89's code.

这是一个 aviatorken89 代码的快速 3 示例。

  1. First create a new file.
  2. Select source cocoa touch class.
  3. Designate subclass of: UITabBarController
  4. Name the class "CustomTabBarController" or whatever you'd like.
  5. Add the following code to the class.

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        var tabFrame:CGRect = self.tabBar.frame
        tabFrame.origin.y = self.view.frame.origin.y
        self.tabBar.frame = tabFrame
    }
    
  6. If you are using storyboard make sure to change the tab bar class to your custom class via the "Identity inspector".

  1. 首先创建一个新文件。
  2. 选择源可可触摸类。
  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. 如果您使用故事板,请确保通过“身份检查器”将标签栏类更改为您的自定义类。

回答by mnemonic23

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
}

storyboardenter image description here

故事板在此处输入图片说明

UPDATE IOS 11 ISSUE

更新 IOS 11 问题

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

上面的代码在 ios 11 上不起作用。所以这是我找到的解决方法。

override func viewDidLayoutSubviews() {
    tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)

    super.viewDidLayoutSubviews()
}

回答by hamed hosiny

subviews of TabbarController hide back of Tabbar . so for fix use this code :

TabbarController 的子视图隐藏在 Tabbar 后面。所以为了修复使用这个代码:

class CustomTabBarController: UITabBarController ,UITabBarControllerDelegate{

    override func viewDidLayoutSubviews() {
        tabBar.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
        super.viewDidLayoutSubviews()
        delegate = self
        selectedViewController?.view.frame.origin = CGPoint(x: 0, y: tabBar.frame.size.height)
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          selectedViewController?.view.frame.origin = CGPoint(x: 0, y: tabBar.frame.size.height)
    }

}

回答by Kivia Martins Brito

Swift 5

斯威夫特 5

Add this code to your UITabBarViewController;

将此代码添加到您的 UITabBarViewController;

  override func viewDidLayoutSubviews() {
    let height = navigationController?.navigationBar.frame.maxY
    tabBar.frame = CGRect(x: 0, y: height ?? 0, width: tabBar.frame.size.width, height: tabBar.frame.size.height)
    super.viewDidLayoutSubviews()
  }

It works for iOS > 13 and iOS < 13

它适用于 iOS > 13 和 iOS < 13

回答by Schemetrical

You can create a custom tab bar by making it yourself, but apple highly discourages mimicking a system control for a function it was not originally intended to do.

您可以通过自己制作来创建自定义标签栏,但 Apple 强烈反对模仿系统控件来实现原本不打算执行的功能。

Once again, I discourage you to do so, because it violates the consistency of your app and system apps. But since I'm at it, here we go:

再一次,我不鼓励你这样做,因为它违反了你的应用程序和系统应用程序的一致性。但既然我已经开始了,那么我们开始吧:

For a custom tab bar, you need to create a view that holds multiple buttons. You also need a container view belowthe UITabBar(because you want the UITabBarto be at the top). When a button is pressed, you change the UIViewControllerinside the container.
Its quite simple, but of course, its strongly not recommended.

对于自定义标签栏,您需要创建一个包含多个按钮的视图。您还需要一个容器视图下方UITabBar(因为你想要的UITabBar是在顶部)。当一个按钮被按下时,你改变UIViewController了容器的内部。
它很简单,但当然,强烈不推荐。

回答by Natasha

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!

如果你想要一个像 UITabBar 这样的东西,那么你如何创建一个自定义的 UIView 并在其中添加任意数量的 UIButtons 。然后通过 Segue 将一些 ViewController 与每个按钮链接起来。完毕!

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

您不需要自定义 UITabBar。正如 aviatorken89 之前所说,它违反了人机界面指南。