美文网首页iOS开发技术
iOS如何获取xib在autolayout之后的frame

iOS如何获取xib在autolayout之后的frame

作者: 不断学习 | 来源:发表于2022-02-18 10:23 被阅读0次

对于视图view来说,如果想获取xib中自动布局后的 frame,需要在layoutSubviews方法中获取自动布局后的frame才是准确的

- (void)layoutSubviews {
 [super layoutSubviews];

 // 不添加这下面两句,获得的尺寸会是xib和storyboard未完成autolayout适配时的尺寸,增加后获取的是适配后的尺寸
 [self.contentView setNeedsLayout];
 [self.contentView layoutIfNeeded];

 // 在这里获取frame

}

对于viewController来说,如果想获取xib中自动布局后的frame,需要在-(void)viewDidLayoutSubviews,-(void)viewWillLayoutSubviews方法中

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    self.moveItem.frame = CGRectMake(0, 0, self.scanView.frame.size.width, 10); //建立视图需要在viewdidload等方法中,在这里指定frame

    [UIView animateKeyframesWithDuration:2 delay:0 options:UIViewKeyframeAnimationOptionRepeat animations:^{

        self.moveItem.frame = CGRectMake(0, self.scanView.frame.size.height - 10, self.scanView.frame.size.width, 10);;

    } completion:nil];//这里的frame值就是正确的,故可以进行动画
}

转自: iOS学习笔记-如何获取xib的autolayout后的frame

相关文章

网友评论

    本文标题:iOS如何获取xib在autolayout之后的frame

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