如何在应用自动布局后获得UIImageView大小



我的故事板有UIImageView,它通过自动布局约束自动定位和缩放。

当应用程序运行时,此UIImageView会根据设备屏幕大小和自动布局约束正确调整大小和位置。

在屏幕上显示UIImageView帧后,如何获取该帧?常见的UIImageView.bounds和UIImageView.frame值返回原始值,这些值在情节提要中使用,不反映新的UIImageView大小。

为了在调整大小后获得UIImageView的正确帧/边界,您需要首先要求auto layout使用[yourImageView layoutIfNeeded]更新该布局。这将解决您的约束并更新您的yourImage.bounds

[myImageView layoutIfNeeded];
NSLog(@"w: %f, h: %f", myImageView.bounds.size.width, myImageView.bounds.size.height);

检查这些方法:

- (void)viewWillAppear:(BOOL)animated {
// view is about to be added to hieararchy
}
- (void)viewDidAppear:(BOOL)animated {
// view was added
}
- (void)viewDidLayoutSubviews {
// VC just laid off its views
}

最新更新