将UITabBar放在顶部

问题描述 投票:25回答:8

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

ios objective-c swift uitabbar
8个回答
36
投票

有可能吗?可以,但是它违反了人机界面准则。

屏幕截图:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9ZZERuVEY0LnBuZyJ9” alt =“ Portrait”> “

代码:

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

8
投票

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

不要忘记将您的自定义类别设置为TabBarControllerenter image description here

结果将是这样:

enter image description here


7
投票

这里是一个有效的aviatorken89代码的3示例。

  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. 如果使用故事板,请确保通过“身份检查器”将选项卡栏类更改为自定义类。


3
投票
override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

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

storyboard enter image description here

更新IOS 11问题

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

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

    super.viewDidLayoutSubviews()
}

3
投票

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

}

1
投票

您可以通过自己制作来创建自定义选项卡,但是苹果强烈不建议将系统控件模仿其原本不打算使用的功能。

再次,我不鼓励您这样做,因为它违反了您的应用程序和系统应用程序的一致性。但是因为我在做,所以我们开始:

对于自定义标签栏,您需要创建一个包含多个按钮的视图。您还需要UITabBar的容器视图以下](因为您希望UITabBar位于顶部)。按下按钮时,将更改容器内的UIViewController。它非常简单,但是当然不建议这样做。


0
投票

Swift 5


-3
投票

如果要在顶部放置类似UITabBar的东西,那么如何创建自定义UIView并在其中添加任意数量的UIButton。然后通过Segue将一些ViewController与每个按钮链接。做完了!

© www.soinside.com 2019 - 2024. All rights reserved.