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