viewdidload中的帧大小不正确,viewdidappear

时间:2014-11-05 15:35:10

标签: objective-c uiviewcontroller viewwillappear viewdidlayoutsubviews viewwilltransitiontosize

使用iOS8。 UIViewController'sviewDidLoadviewDidAppear视图的框架尺寸不正确。 我可以从viewDidLayoutSubviews获得正确的帧大小并执行视图更新(我需要使布局无效),但是使用viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator处理旋转变得毫无意义,并且2种方法将其公之于众。

理想情况下willTransitionToSize将被调用,而不是didLayoutSubviews,但不是两者,因为我不知道我是否真的需要使布局无效并执行无意义的计算,布局等。

我想避免使用与旋转和帧大小变化有关的各种魔术标记。

在发生任何这种情况之前,有没有办法获得视图控制器视图的“真实”大小?

修改

以下是一些示例代码:

-(void)viewDidLoad {
  [super viewDidLoad];
  NSLog(@"My Frame = %@",NSStringFromCGRect(self.view.frame)); // prints 320x568 i.e. the whole screen
  // more setup code...
  self.someView = [SomeClass alloc] initWithFrame:self.view.bounds];
  self.someView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  // add some more views and masks
}

-(void)viewDidAppear {
  [super viewDidAppear];
  NSLog(@"My Frame = %@",NSStringFromCGRect(self.view.frame)); // prints 320x568 i.e. the whole screen

 // view "sticks" out the bottom of the screen still - don't know if I need to layout "self.someView". I could add "viewWillLayoutSubviews" but than rotation wouldn't be animated - because I wouldn't know which condition to take...
}

// if the view controller rotates than need to update appearance in a nice animated way
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self updateAppearance];
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

    }];
}

0 个答案:

没有答案