在加载视图中创建视图并设置其框架,但框架自动更改

时间:2012-07-03 06:49:28

标签: iphone ios uiview

为什么视图框架会自动更改?

请参阅此代码:

- (void)loadView{
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(-10,0,340,480)];
    self.view = sv;
    NSLog(@"sv frame = %@", NSStringFromCGRect(self.view))
}

- (void)viewWillAppear{
  NSLog(@"view frame = %@", NSStringFromCGRect(self.view));
}

在我的演示中 输出: sv frame = {{-10,0},{340,480}}

view frame = {{0,0},{320,480}}

应该是:view frame = {{-10,0},{340,480}}

但是在PhotoScroller演示(2010 WWDC 104)中,它是正确的。这太奇怪了。

3 个答案:

答案 0 :(得分:2)

以这种方式更改两种方法:

- (void)loadView{
   UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectZero];
   self.view = sv;
}

- (void)viewWillAppear:(BOOL)animated {
   self.view.frame = CGRectMake(-10,0,340,480);
}

编辑:几何必须至少在viewWillAppear:以及UIViewController的事件生命周期中设置。自动调整蒙版也可以更改几何体。

答案 1 :(得分:1)

- (void)loadView
{    
    UISCrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view = scrollView;
}

答案 2 :(得分:-1)

试试这个:

- (void)loadView{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(-10,0,340,480)];
    [view addSubview:sv];
    sv.autoresizingMak=UIViewAutoresizingMaskFlexibleWidth|UIViewAutoresizingMaskFlexibleHeight;
    self.view = view;
}