Skip to content

Instantly share code, notes, and snippets.

@bawn
Created October 31, 2018 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bawn/68e9c20caf785ba3ce20efd3d38a18a2 to your computer and use it in GitHub Desktop.
Save bawn/68e9c20caf785ba3ce20efd3d38a18a2 to your computer and use it in GitHub Desktop.
修复 iOS 12.1 上 UITabBar 里面的 items 偏移问题
import UIKit
class HQTabBar: UITabBar {
var itemFrames = [CGRect]()
var tabBarItems = [UIView]()
override func layoutSubviews() {
super.layoutSubviews()
if itemFrames.isEmpty, let UITabBarButtonClass = NSClassFromString("UITabBarButton") as? NSObject.Type {
tabBarItems = subviews.filter({$0.isKind(of: UITabBarButtonClass)})
tabBarItems.forEach({itemFrames.append($0.frame)})
}
if !itemFrames.isEmpty, !tabBarItems.isEmpty, itemFrames.count == items?.count {
tabBarItems.enumerated().forEach({$0.element.frame = itemFrames[$0.offset]})
}
}
}
@zjfjack
Copy link

zjfjack commented Nov 2, 2018

Hi, 感谢提供方案。
请问下如果不使用storyboard,用纯代码怎么可以替换原来的tab bar到我们自定义的tab bar?
用了override tab bar 结果layoutSubviews都不调用。
用setvalue(mTabBar, "tabBar")是没偏移问题,但是好多tab Bar原先的功能又没了

@xieweizhi
Copy link

In viewDidLoad

setValue(HQTabBar(), forKey: "tabBar")

@xuezhenchao
Copy link

请问下有没有oc版的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment