iOS:如何获取xib的autolayout后的frame

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

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

- (void)layoutSubviews
{
 [super layoutSubviews];
 [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值就是正确的,故可以进行动画
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。