美文网首页
iPhone X的tabbar push时图片下移问题

iPhone X的tabbar push时图片下移问题

作者: 北纬49度的雨 | 来源:发表于2017-11-21 12:17 被阅读0次

    适配iPhone X时出现了以下问题

    左侧为正常效果;右侧为iPhone Xpresent后dismiss回来,然后再push后的效果

    估计的原因是iPhone X上tabbar的高度由原来的49变为83,但是在UITabBarController中只有在viewdidAppear以后才能获取到正确的高度,而自定义的tabbar中是可以根据iOS11的safeAreaInsetsDidChange方法重新设置


    后来改了好久,终于在stack overflow上找到了解决办法,链接地址:https://stackoverflow.com/questions/46232929/why-page-push-animation-tabbar-moving-up-in-the-iphone-x/47225653#4722565


    解决办法如下,新建一个类wls_baseTabbar继承自UITabbar

    wls_baseTabbar.m 文件

    #import "tgq_baseTabbar.h"

    @implementation tgq_baseTabbar

    {

           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

    然后在你自定义的UITabBarController中

    - (void)viewDidLoad {

    [super viewDidLoad];

    //1.设置tabbar

    tgq_baseTabbar *bar = [[tgq_baseTabbar alloc] init]

    [self setValue:bar forKey:@"tabBar"];

    //2.添加子控制器

    [self setChildVC];

    }

    相关文章

      网友评论

          本文标题:iPhone X的tabbar push时图片下移问题

          本文链接:https://www.haomeiwen.com/subject/teoivxtx.html