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)
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)
结果将是这样的:
这是aviatorken89的代码的一个工作swift 3示例.
将以下代码添加到类中.
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)如果您使用的是故事板,请确保通过"身份检查器"将标签栏类更改为您的自定义类.
小智 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