In some cases, the system automatically adjusts the contentInset and uiscrollviewinset of UIScrollView.

Source: Internet
Author: User

In some cases, the system automatically adjusts the contentInset and uiscrollviewinset of UIScrollView.

 

Scenario

If a controller (ViewController) is managed by the navigation controller and its first sub-control is UIScrollView, the system will adjust the contentInset of UIScrollView by default.

UIEdgeInsetsMake (64, 0, 0, 0) // has a navigation bar

UIEdgeInsetsMake (20, 0, 0, 0) // no navigation bar

If the navigation controller is managed by UITabBarController

UIEdgeInsetsMake (64, 0, 49, 0) // The navigation controller is managed by UITabBarController.

How to cancel the system's default automatic adjustment function

  ViewController.automaticallyAdjustsScrollViewInsets = NO;

Code verification -->

//// ViewController. m # import "ViewController. h "@ interface ViewController () @ property (weak, nonatomic) IBOutlet UIScrollView * scrollView; @ property (nonatomic, weak) UISwitch * customSwitch; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; UISwitch * customSwitch = [[UISwitch alloc] init]; [self. scrollView addSubview: customSwitch]; self. customSwitch = customSwitch; dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (1 * NSEC_PER_SEC), interval (), ^ {NSLog (@ "% @", self. scrollView); NSLog (@ "% @", NSStringFromUIEdgeInsets (self. scrollView. contentInset); NSLog (@ "% @", NSStringFromCGRect (self. customSwitch. bounds);} // print the result/*. Only UInavigationController <UIScrollView: 0x7fbbd301fa00; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = RM + BM; gestureRecognizers = <NSArray: arrays>; layer = <CALayer: 0x7fbbd25176a0>; contentOffset: {0,-64}; contentSize: {0, 0}> {64, 0, 0, 0} {0, 0}, {51, 31} * // case 2 only UInavigationController, however, the navigation bar <UIScrollView: 0x7f80cb024200; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = RM + BM; Comment = <NSArray: 0x7f80c9563330>; layer = <CALayer: 0x7f80c955f090>; contentOffset: {0,-20}; contentSize: {0, 0 }>{ 20, 0, 0, 0} {0, 0}, {51, 31} * // Case 3: Both UInavigationController and UITabBarController <UIScrollView: 0x7fe60903fa00; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = RM + BM; gestureRecognizers = <NSArray: arrays>; layer = <CALayer: 0x7fe608f06320>; contentOffset: {0,-64}; contentSize: {0, 0}> {64, 0, 49, 0} {0, 0}, {51, 31} * // The above uses the latency method (the method in GCD) the output in the viewDidLoad method is not the final result. You can also use the following method to print-(void) viewDidLayoutSubviews {// NSLog (@ "% @", self. scrollView); // NSLog (@ "% @", NSStringFromUIEdgeInsets (self. scrollView. contentInset);}-(void) viewDidAppear :( BOOL) animated {// NSLog (@ "% @", self. scrollView); // NSLog (@ "% @", NSStringFromUIEdgeInsets (self. scrollView. contentInset);} @ end

 

Related Article

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.