如何获得我正在控制的 View 或 subview 的实际尺寸?例如:
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,100)];
[self addSubview:firstView];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 230, 120)];
[firstView addSubview:button];
如您所见,我的 button
对象超出了它所添加到的 View 的尺寸。
考虑到这一点(假设有许多对象被添加为 subview ),如何确定 firstView
的实际宽度和高度?
最佳答案
如果你想让按钮的宽度和高度与 firstView 相同,你应该像这样使用 UIView 的 bounds 属性:
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,100)];
[self addSubview:firstView];
UIButton *button = [[UIButton alloc] initWithFrame:firstView.bounds];
[firstView addSubview:button];
要获得 firstView 的宽度/高度,您可以这样做:
CGSize size = firstView.bounds.size;
NSInteger width = size.width;
NSInteger height = size.height;
关于ios - 获取 iOS View 的实际尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14854286/