如何从View Controller中获取UIView的大小? [英] How do I get the size of a UIView from within the View Controller?

查看:276
本文介绍了如何从View Controller中获取UIView的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UIViewController的 viewDidLoad 方法内:

Inside the viewDidLoad method of a UIViewController:

NSLog(@"frame: %f %f", self.view.frame.size.width, self.view.frame.size.height);

设备处于横向状态:

frame: 748.000000 1024.000000

设备处于纵向:

frame: 768.000000 1004.000000


推荐答案

viewDidLoad 中,框架可能没有调整到影响其最终尺寸的所有事情,例如旋转,其他视图等。通常最好只在这里创建和设置对象;你可以在 viewWillLayoutSubviews 中检查最终的框架。

In viewDidLoad, the frame may not have adjusted to all of the things that affect its final size, such as rotation, other views, etc. It's generally best to only create and set up your objects here; you can inspect the finalized frame in viewWillLayoutSubviews.

这篇关于如何从View Controller中获取UIView的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆