iphone x 上的标签栏控制器
Tab Bar controller on iphone x
我现在这是重复的问题,但没有任何解决方案可以解决我的问题。我是 IOS 的新手,我在使用 swift 的项目中使用 TabBar 控制器 4. TabBar 工作完美,但在 Iphone X 上存在设计问题。故事板预览在 IphonX 上可以,但模拟器和设备显示设计问题..查看我的故事板
我没有为 TabBar 使用任何自定义视图。我还检查了我项目中使用的安全区域布局指南。
我的 TabBar 控制器代码是
import UIKit
class TabBarSingleton: NSObject {
static let sharedInstance = TabBarSingleton()
var tabBarObject : UITabBar?
override init(){}
}
class TabBarViewController:
UITabBarController,UITabBarControllerDelegate {
let userID = PSStateManager.sharedManager.userId
override func viewDidLoad() {
super.viewDidLoad()
TabBarSingleton.sharedInstance.tabBarObject = self.tabBar
for vc in viewControllers!{
vc.tabBarItem.imageInsets=UIEdgeInsetsMake(-2, 0, +2, 0)
}
self.tabBar.unselectedItemTintColor = UIColor.init(hexString: "#5E5E5E")
self.tabBar.tintColor=UIColor.init(hexString: "#36C4E5")
self.delegate = self
self.tabBar.barTintColor = themeColor
self.tabBar.isTranslucent = false
self.changeSelectionColor()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
patientStatisticCallerFunc(userID: userID!) { (model) in
return
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let navVC = selectedViewController as! PSNavigationViewController
navVC.popToRootViewController(animated: false)
navVC.updateNavBar()
}
func changeSelectionColor() {
let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.white, size: tabBarItemSize).resizableImage(withCapInsets: .zero)
// remove default border
tabBar.frame.size.width = self.view.frame.width + 4
tabBar.frame.origin.x = -2
// tab bar item tint color for selected and unselected and font size
let colorNormal : UIColor = UIColor.init(hexString: "#5E5E5E")
let selectedColor : UIColor = UIColor.init(hexString: "#36C4E5")
let titleFontAll : UIFont = UIFont(name: "segoeui", size: 9.0)!
let attributesNormal = [
NSAttributedStringKey.foregroundColor : colorNormal,
NSAttributedStringKey.font : titleFontAll
]
let attributesSelected = [
NSAttributedStringKey.foregroundColor : selectedColor,
NSAttributedStringKey.font : titleFontAll
]
UITabBarItem.appearance().setTitleTextAttributes(attributesNormal, for: .normal)
UITabBarItem.appearance().setTitleTextAttributes(attributesSelected, for: .selected)
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
self.changeSelectionColor()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
self.tabBar.insetsLayoutMarginsFromSafeArea = true
}
self.tabBar.invalidateIntrinsicContentSize()
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
let tabViewControllers = tabBarController.viewControllers!
guard let toIndex = tabViewControllers.index(of: viewController) else {
return false
}
animateToTab(toIndex: toIndex)
return true
}
func animateToTab(toIndex: Int) {
let tabViewControllers = viewControllers!
let fromView = selectedViewController!.view
let toView = tabViewControllers[toIndex].view
let fromIndex = tabViewControllers.index(of: selectedViewController!)
guard fromIndex != toIndex else {return}
// Add the toView to the tab bar view
fromView?.superview!.addSubview(toView!)
// Position toView off screen (to the left/right of fromView)
let screenWidth = UIScreen.main.bounds.size.width;
let scrollRight = toIndex > fromIndex!;
let offset = (scrollRight ? screenWidth : -screenWidth)
toView?.center = CGPoint(x: (fromView?.center.x)! + offset, y: (toView?.center.y)!)
// Disable interaction during animation
view.isUserInteractionEnabled = false
UIView.animate(withDuration: mediumTranstionDuration, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
// Slide the views by -offset
fromView?.center = CGPoint(x: (fromView?.center.x)! - offset, y: (fromView?.center.y)!);
toView?.center = CGPoint(x: (toView?.center.x)! - offset, y: (toView?.center.y)!);
}, completion: { finished in
// Remove the old view from the tabbar view.
fromView?.removeFromSuperview()
self.selectedIndex = toIndex
self.view.isUserInteractionEnabled = true
})
}
func changeLabelColors(inView: UIView) {
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我正面临这个问题
我遇到了同样的问题,我的 UITabBar
、
使用这个 class 解决了
@IBDesignable class CustomTabBar: UITabBar {
@IBInspectable var height: CGFloat = 0.0
override func sizeThatFits(_ size: CGSize) -> CGSize {
var sizeThatFits = super.sizeThatFits(size)
if height > 0.0 {
sizeThatFits.height = height
if(isiPhoneXScreen()) {
sizeThatFits.height = height + 35
}
}
return sizeThatFits
}
}
那么你只需要在故事板
中为你的CustomTabBar
class替换正常的UITabBar
将此 vc.tabBarItem.imageInsets=UIEdgeInsetsMake(-2, 0, +2, 0)
更改为 vc.tabBarItem.imageInsets=UIEdgeInsetsMake(0, 0, 0, 0)
还有这个 UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
到 UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 0)
我现在这是重复的问题,但没有任何解决方案可以解决我的问题。我是 IOS 的新手,我在使用 swift 的项目中使用 TabBar 控制器 4. TabBar 工作完美,但在 Iphone X 上存在设计问题。故事板预览在 IphonX 上可以,但模拟器和设备显示设计问题..查看我的故事板
我没有为 TabBar 使用任何自定义视图。我还检查了我项目中使用的安全区域布局指南。
我的 TabBar 控制器代码是
import UIKit
class TabBarSingleton: NSObject {
static let sharedInstance = TabBarSingleton()
var tabBarObject : UITabBar?
override init(){}
}
class TabBarViewController:
UITabBarController,UITabBarControllerDelegate {
let userID = PSStateManager.sharedManager.userId
override func viewDidLoad() {
super.viewDidLoad()
TabBarSingleton.sharedInstance.tabBarObject = self.tabBar
for vc in viewControllers!{
vc.tabBarItem.imageInsets=UIEdgeInsetsMake(-2, 0, +2, 0)
}
self.tabBar.unselectedItemTintColor = UIColor.init(hexString: "#5E5E5E")
self.tabBar.tintColor=UIColor.init(hexString: "#36C4E5")
self.delegate = self
self.tabBar.barTintColor = themeColor
self.tabBar.isTranslucent = false
self.changeSelectionColor()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
patientStatisticCallerFunc(userID: userID!) { (model) in
return
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let navVC = selectedViewController as! PSNavigationViewController
navVC.popToRootViewController(animated: false)
navVC.updateNavBar()
}
func changeSelectionColor() {
let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.white, size: tabBarItemSize).resizableImage(withCapInsets: .zero)
// remove default border
tabBar.frame.size.width = self.view.frame.width + 4
tabBar.frame.origin.x = -2
// tab bar item tint color for selected and unselected and font size
let colorNormal : UIColor = UIColor.init(hexString: "#5E5E5E")
let selectedColor : UIColor = UIColor.init(hexString: "#36C4E5")
let titleFontAll : UIFont = UIFont(name: "segoeui", size: 9.0)!
let attributesNormal = [
NSAttributedStringKey.foregroundColor : colorNormal,
NSAttributedStringKey.font : titleFontAll
]
let attributesSelected = [
NSAttributedStringKey.foregroundColor : selectedColor,
NSAttributedStringKey.font : titleFontAll
]
UITabBarItem.appearance().setTitleTextAttributes(attributesNormal, for: .normal)
UITabBarItem.appearance().setTitleTextAttributes(attributesSelected, for: .selected)
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
self.changeSelectionColor()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
self.tabBar.insetsLayoutMarginsFromSafeArea = true
}
self.tabBar.invalidateIntrinsicContentSize()
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
let tabViewControllers = tabBarController.viewControllers!
guard let toIndex = tabViewControllers.index(of: viewController) else {
return false
}
animateToTab(toIndex: toIndex)
return true
}
func animateToTab(toIndex: Int) {
let tabViewControllers = viewControllers!
let fromView = selectedViewController!.view
let toView = tabViewControllers[toIndex].view
let fromIndex = tabViewControllers.index(of: selectedViewController!)
guard fromIndex != toIndex else {return}
// Add the toView to the tab bar view
fromView?.superview!.addSubview(toView!)
// Position toView off screen (to the left/right of fromView)
let screenWidth = UIScreen.main.bounds.size.width;
let scrollRight = toIndex > fromIndex!;
let offset = (scrollRight ? screenWidth : -screenWidth)
toView?.center = CGPoint(x: (fromView?.center.x)! + offset, y: (toView?.center.y)!)
// Disable interaction during animation
view.isUserInteractionEnabled = false
UIView.animate(withDuration: mediumTranstionDuration, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
// Slide the views by -offset
fromView?.center = CGPoint(x: (fromView?.center.x)! - offset, y: (fromView?.center.y)!);
toView?.center = CGPoint(x: (toView?.center.x)! - offset, y: (toView?.center.y)!);
}, completion: { finished in
// Remove the old view from the tabbar view.
fromView?.removeFromSuperview()
self.selectedIndex = toIndex
self.view.isUserInteractionEnabled = true
})
}
func changeLabelColors(inView: UIView) {
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我正面临这个问题
我遇到了同样的问题,我的 UITabBar
、
@IBDesignable class CustomTabBar: UITabBar {
@IBInspectable var height: CGFloat = 0.0
override func sizeThatFits(_ size: CGSize) -> CGSize {
var sizeThatFits = super.sizeThatFits(size)
if height > 0.0 {
sizeThatFits.height = height
if(isiPhoneXScreen()) {
sizeThatFits.height = height + 35
}
}
return sizeThatFits
}
}
那么你只需要在故事板
中为你的CustomTabBar
class替换正常的UITabBar
将此 vc.tabBarItem.imageInsets=UIEdgeInsetsMake(-2, 0, +2, 0)
更改为 vc.tabBarItem.imageInsets=UIEdgeInsetsMake(0, 0, 0, 0)
还有这个 UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
到 UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 0)