0

使用iOS8。视图的帧大小在和UIViewController's中不正确。我可以从中获取正确的帧大小并执行视图更新(我需要使布局无效),但是处理旋转变得毫无意义,并且这两种方法将其淘汰。viewDidLoadviewDidAppearviewDidLayoutSubviewsviewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

理想情况下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) {

    }];
}
4

0 回答 0