Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

iphone - View frame changes between viewWillAppear: and viewDidAppear:

I have discovered a strange behavior in my application, where a connected IBOutlet has its connected view's frame between the calls in my view controller to viewWillAppear: and viewDidAppear:. Here is the relevant code in my UIViewController subclass:

-(void)viewWillAppear:(BOOL)animated {
    NSLog(@"%@", self.scrollView);
}

-(void)viewDidAppear:(BOOL)animated {
    NSLog(@"%@", self.scrollView);
}

and the resulting log output:

MyApp[61880:c07] <UIScrollView: 0x1057eff0; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x10580100>; layer = <CALayer: 0x1057f210>; contentOffset: {0, 0}>
MyApp[61880:c07] <UIScrollView: 0x1057eff0; frame = (0 44; 320 416); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x10580100>; layer = <CALayer: 0x1057f210>; contentOffset: {0, 0}>

Which clearly shows that the frame is changing between the two calls. I wanted to do setup with the view in the viewDidLoad method, but if the content is not available for me to change until it is on the screen, that seems pretty useless. What could be happening?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the documentation:

viewWillAppear:

Notifies the view controller that its view is about to be added to a view hierarchy.

viewDidAppear:

Notifies the view controller that its view was added to a view hierarchy.

As a result the frames of the subviews aren't yet set in the viewWillAppear:

The appropriate method to modify your UI before the view is presented to the screen is:

viewDidLayoutSubviews

Notifies the view controller that its view just laid out its subviews.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...