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

对于视图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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容