If you set a UIViewControlller in UINavigationController and the first sub-view of UIViewController is UIScrollView, all the subviews in UIScrollview will be moved down.Size Problem title = ios7UIScrollViewSize Problem> code:
-(Void) viewDidLoad
{
[Super viewDidLoad];
UIScrollView * tempScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 64,320,200)];
[TempScroll setBackgroundColor: [UIColor grayColor];
[TempScroll setContentSize: CGSizeMake (self. view. bounds. size. width, self. view. bounds. size. height)];
[Self. view addSubview: tempScroll];
UIButton * tempButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[TempButton setBackgroundColor: [UIColor redColor];
[TempButton setTitle: @ subView A forState: UIControlStateNormal];
[TempButton setFrame: CGRectMake (80, 0, 80,100)];
NSLog (@ % d, tempScroll. subviews. count );
[TempScroll addSubview: tempButton];
}
After verification code, I found that ios7 has a mechanism
When both navigationBar and statusBar are displayed, the current VC of Navigation, the first child view at the root of the Child view tree of his VC view, if it is Scrollview, all subviews of this scrollview are moved 64 pixels down.
After discovering this mechanism, how can we fix it?
There are two correction schemes
1. Move all subviews of scrollview up to 64 pixels.
UIView * targetView = self. view;
While (targetView. subviews. count> 0 &&! [TargetView isKindOfClass: [UIScrollView class]) {
TargetView = [targetView. subviews objectAtIndex: 0];
}
If ([targetView isKindOfClass: [UIScrollView class]) {
NSLog (@ you are a scrollview );
CGSize tempSize = (UIScrollView *) targetView). contentSize;
TempSize. height-= 64;
[(UIScrollView *) targetView setContentSize: tempSize];
For (UIView * subView in targetView. subviews ){
CGRect tempRect = subView. frame;
TempRect. origin. y-= 64;
[SubView setFrame: tempRect];
}
}
2. Changing the status of a scrollView is not the first subview at the root of the subview tree.
-(Void) viewDidLoad
{
[Super viewDidLoad];
UIView * tempBackGround = [[UIView alloc] initWithFrame: self. view. bounds];
[Self. view addSubview: tempBackGround];
UIScrollView * tempScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 64,320,200)];
[TempScroll setBackgroundColor: [UIColor grayColor];
[TempScroll setContentSize: CGSizeMake (self. view. bounds. size. width, self. view. bounds. size. height)];
[Self. view addSubview: tempScroll];
UIButton * tempButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[TempButton setBackgroundColor: [UIColor redColor];
[TempButton setTitle: @ subView A forState: UIControlStateNormal];
[TempButton setFrame: CGRectMake (80, 0, 80,100)];
NSLog (@ % d, tempScroll. subviews. count );
[TempScroll addSubview: tempButton];
}
Corrected
UIScrollViewSize Problem title = ios7UIScrollViewDimensions>