Ios7 UIScrollView Size Problem

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.