【发布时间】:2014-01-08 12:13:09
【问题描述】:
在 iOS7 之前,我为 UIView 的框架定义的任何 CGRect 都可以正常工作。
例如:
imageView.frame=CGRectMake(20,20,80,80);
在屏幕左上角完美地留下了 20 x 20 的边距。
有趣的是,如果我添加另一个 UIView:
nextView.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+10,20,100,100);
nextView 出现在 imageView 附近,因此它以某种方式接受第二个视图的 X 参数,但也不接受该视图的 Y 坐标。
现在,我必须使用viewWillLayoutSubviews 来安排所有这些坐标。否则,例如,imageView 的框架会自动变为0,0,80,80。 (它捕捉到左上角)
我没有使用任何笔尖,我什至还尝试使用自定义 initWithFrame 方法创建 UIViewController 以在 loadView 或 viewDidLoad 之前设置 CGRect 但这没关系。
有什么东西可以自动覆盖 x,y 的视图吗?像自动布局系统等?我不想使用viewWillLayoutSubviews 方法重新排列所有这些坐标。
这里发生了什么?
更新 1
- (id)initWithFrame:(CGRect) frame
{
self = [super init];
if (self) {
self.frame=frame; //frame is custom variable to hold it until loadViews method
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)loadView{
UIView *view=[[UIView alloc]initWithFrame:_frame];
view.backgroundColor=[UIColor blackColor];
self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 80,80)];
self.imageView.backgroundColor=[UIColor redColor];
[view addSubview:_imageView];
self.textView=[[UITextView alloc]initWithFrame:CGRectMake(self.imageView.frame.origin.x+self.imageView.frame.size.width+10, self.imageView.frame.origin.y, view.frame.size.width-20-(self.imageView.frame.origin.x+self.imageView.frame.size.width+10), view.frame.size.height-self.imageView.frame.origin.y-20)];
[view addSubview:_textView];
self.view=view;
}
我在 initWithFrame 和 viewDidLoad 中尝试了类似的代码。结果是一样的。
更新 2
我在问题中错误地写了“layoutSubviews”而不是“viewWillLayoutSubviews”,所以我修复了它:我不是在创建自定义 UIView 而是 UIViewController,所以在问题中它必须是“viewWillLayoutSubviews”。
【问题讨论】:
-
你是如何定义你的视图的,它在故事板上吗?
-
设置 imageView.bound 而不是 imageView.frame
-
@ldindu 我没有使用任何情节提要,它是一个自定义 UIViewController,我在其中手动在代码中创建所有视图。
-
@CoolMonster 我现在试过了,但也没用。
-
你在哪里定义了这些图像和下一个视图?
标签: ios uiview uiviewcontroller layoutsubviews