将UITabBar定位在顶部

Alg*_*Dev 24 objective-c uitabbar ios swift

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

klc*_*r89 35

可能吗?当然,但它违反了人机界面指南.

截图:

肖像 景观 故事板

码:

TabController.h:

#import <UIKit/UIKit.h>

@interface TabController : UITabBarController <UITabBarControllerDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • @AlgroDev我猜你是一个Android Dev来到iOS.需要注意的事情ios!= Android.它们是两个完全不同的操作系统.你可能有一些适用于Android的东西,但这并不意味着它适用于iOS.事实上它可能与iOS完全冲突.在iOS中,用户希望标签栏位于底部,导航栏位于顶部.小心你不要通过向用户展示那些意想不到的东西来推迟用户. (2认同)

Mil*_*nia 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)
    }
Run Code Online (Sandbox Code Playgroud)

不要忘记将自定义类设置为 TabBarController 在此输入图像描述

结果将是这样的:

在此输入图像描述


Leo*_*Leo 7

这是aviatorken89的代码的一个工作swift 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
    }
    
    Run Code Online (Sandbox Code Playgroud)
  6. 如果您使用的是故事板,请确保通过"身份检查器"将标签栏类更改为您的自定义类.


小智 5

斯威夫特 5

将此代码添加到您的 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()
  }
Run Code Online (Sandbox Code Playgroud)

它适用于 iOS > 13 和 iOS < 13