1

为什么视图的框架自动改变了?

看到这个代码:

- (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}}

视图框架 = {{0, 0}, {320, 480}}

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

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

4

3 回答 3

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的事件生命周期中设置几何。自动调整蒙版大小也可以更改几何形状。

于 2012-07-03T06:54:11.197 回答
1
- (void)loadView
{    
    UISCrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view = scrollView;
}
于 2012-07-03T14:35:50.677 回答
-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;
}
于 2012-07-03T11:06:08.277 回答