标签栏控制器中的图像和标题重叠 iPhone X?
Image and Title in Tab Bar Controller get’s overlap iPhone X?
Tab Bar Controller 内的 Tab Bar Item 在 iPhone X 中重叠。
有什么解决方案吗?
我认为这不是一个完美的答案,但下面的代码解决了我的问题。
- 为你的 tabbarcontroller 创建一个子类
在viewDidLoad方法中添加代码
设宽度=tabBar.bounds.width
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 2436:
let tabSize = CGSize(width: width/5, height: 83)
UIGraphicsBeginImageContext(tabSize)
selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height))
selectionImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
default:
let tabSize = CGSize(width: width/5, height: 49)
UIGraphicsBeginImageContext(tabSize)
selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height))
selectionImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
希望对您有所帮助!
- 发现问题与动画有关。
- 我能够解决这个问题。
- 通过使用 CustomTabBar 从 Interface builder 子类化 UITabBarClass
Swift代码
- Class CustomTabBarClass.swift
class CustomTabBarClass: UITabBar {
var oldSafeAreaInsets = UIEdgeInsets.zero
@available(iOS 11.0, *)
override func safeAreaInsetsDidChange() {
super.safeAreaInsetsDidChange()
if oldSafeAreaInsets != safeAreaInsets {
oldSafeAreaInsets = safeAreaInsets
invalidateIntrinsicContentSize()
superview?.setNeedsLayout()
superview?.layoutSubviews()
}
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
var size = super.sizeThatFits(size)
if #available(iOS 11.0, *) {
let bottomInset = safeAreaInsets.bottom
if bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90) {
size.height += bottomInset
}
}
return size
}
override var frame: CGRect {
get {
return super.frame
}
set {
var tmp = newValue
if let superview = superview, tmp.maxY !=
superview.frame.height {
tmp.origin.y = superview.frame.height - tmp.height
}
super.frame = tmp
}
}
}
Objective C代码
- CustomTabBarClass.m
@implementation CustomTabBarClass{
UIEdgeInsets oldSafeAreaInsets;
}
- (void)awakeFromNib {
[super awakeFromNib];
oldSafeAreaInsets = UIEdgeInsetsZero;
}
- (void)safeAreaInsetsDidChange {
[super safeAreaInsetsDidChange];
if (!UIEdgeInsetsEqualToEdgeInsets(oldSafeAreaInsets, self.safeAreaInsets)) {
[self invalidateIntrinsicContentSize];
if (self.superview) {
[self.superview setNeedsLayout];
[self.superview layoutSubviews];
}
}
}
- (CGSize)sizeThatFits:(CGSize)size {
size = [super sizeThatFits:size];
if (@available(iOS 11.0, *)) {
float bottomInset = self.safeAreaInsets.bottom;
if (bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90)) {
size.height += bottomInset;
}
}
return size;
}
- (void)setFrame:(CGRect)frame {
if (self.superview) {
if (frame.origin.y + frame.size.height != self.superview.frame.size.height) {
frame.origin.y = self.superview.frame.size.height - frame.size.height;
}
}
[super setFrame:frame];
}
@end
Tab Bar Controller 内的 Tab Bar Item 在 iPhone X 中重叠。
有什么解决方案吗?
我认为这不是一个完美的答案,但下面的代码解决了我的问题。
- 为你的 tabbarcontroller 创建一个子类
在viewDidLoad方法中添加代码
设宽度=tabBar.bounds.width
if UIDevice().userInterfaceIdiom == .phone { switch UIScreen.main.nativeBounds.height { case 2436: let tabSize = CGSize(width: width/5, height: 83) UIGraphicsBeginImageContext(tabSize) selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height)) selectionImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() default: let tabSize = CGSize(width: width/5, height: 49) UIGraphicsBeginImageContext(tabSize) selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height)) selectionImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } }
希望对您有所帮助!
- 发现问题与动画有关。
- 我能够解决这个问题。
- 通过使用 CustomTabBar 从 Interface builder 子类化 UITabBarClass
Swift代码 - Class CustomTabBarClass.swift
class CustomTabBarClass: UITabBar {
var oldSafeAreaInsets = UIEdgeInsets.zero
@available(iOS 11.0, *)
override func safeAreaInsetsDidChange() {
super.safeAreaInsetsDidChange()
if oldSafeAreaInsets != safeAreaInsets {
oldSafeAreaInsets = safeAreaInsets
invalidateIntrinsicContentSize()
superview?.setNeedsLayout()
superview?.layoutSubviews()
}
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
var size = super.sizeThatFits(size)
if #available(iOS 11.0, *) {
let bottomInset = safeAreaInsets.bottom
if bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90) {
size.height += bottomInset
}
}
return size
}
override var frame: CGRect {
get {
return super.frame
}
set {
var tmp = newValue
if let superview = superview, tmp.maxY !=
superview.frame.height {
tmp.origin.y = superview.frame.height - tmp.height
}
super.frame = tmp
}
}
}
Objective C代码 - CustomTabBarClass.m
@implementation CustomTabBarClass{
UIEdgeInsets oldSafeAreaInsets;
}
- (void)awakeFromNib {
[super awakeFromNib];
oldSafeAreaInsets = UIEdgeInsetsZero;
}
- (void)safeAreaInsetsDidChange {
[super safeAreaInsetsDidChange];
if (!UIEdgeInsetsEqualToEdgeInsets(oldSafeAreaInsets, self.safeAreaInsets)) {
[self invalidateIntrinsicContentSize];
if (self.superview) {
[self.superview setNeedsLayout];
[self.superview layoutSubviews];
}
}
}
- (CGSize)sizeThatFits:(CGSize)size {
size = [super sizeThatFits:size];
if (@available(iOS 11.0, *)) {
float bottomInset = self.safeAreaInsets.bottom;
if (bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90)) {
size.height += bottomInset;
}
}
return size;
}
- (void)setFrame:(CGRect)frame {
if (self.superview) {
if (frame.origin.y + frame.size.height != self.superview.frame.size.height) {
frame.origin.y = self.superview.frame.size.height - frame.size.height;
}
}
[super setFrame:frame];
}
@end