In iOS 7, if a uiviewcontroller's self. the first sub-view of the view is uiscollview. When the uiviewcontroller is pushed or initwithrootcontroller becomes the Controller Controlled by uinavigationcontroller, all sub-views of the uiscollview of the view of the uiviewcontroller will be moved down to 64px.
The premise for this 64 PX move down is that the navigationbar and statusbar are not hidden. For statusbar, the default height is 20px, and for navigatibar, the default height is 44px. The following is a comparison
Instance:
Skip without navigation
1. In the appdelegate. M file:
OBJ-C code 650) This. width = 650; "class =" star "src =" http://unremittingly.iteye.com/images/icon_star.png "alt =" add to favorites code "style =" border: 0px; "/>
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
Self. Window = [[uiwindow alloc] initwithframe: [uiscreen mainscreen] bounds];
Self. Window. backgroundcolor = [uicolor whitecolor];
// The code added for the following two actions
Viewcontroller * rootviewcontroller = [[viewcontroller alloc] init];
[Self. Window setrootviewcontroller: rootviewcontroller];
[Self. Window makekeyandvisible];
Return yes;
}
2. In viewcontroller. M:
OBJ-C code 650) This. width = 650; "class =" star "src =" http://unremittingly.iteye.com/images/icon_star.png "alt =" add to favorites code "style =" border: 0px; "/>
-(Void) viewdidload
{
[Super viewdidload];
Uiscrollview * scrollview = [[uiscrollview alloc] initwithframe: cgrectmake (30.0, 64.0, 260.0, 300.0)];
[Scrollview setbackgroundcolor: [uicolor redcolor];
Uiview * view = [[uiview alloc] initwithframe: scrollview. bounds];
[View setbackgroundcolor: [uicolor bluecolor];
[Scrollview addsubview: View];
[Self. View addsubview: scrollview];
}
3. Results After running:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/23/32/wKioL1M02bjAygEmAABp-pZVp3I608.jpg "style =" float: none; "Title =" 489bf0c9-3ca7-4af9-955e-0fa421f36eb1.png "alt =" wKioL1M02bjAygEmAABp-pZVp3I608.jpg "/>
In this case, scrollview is not affected.
4. Now, use uinavigationcontroller to change the two lines added to appdelegate. m:
OBJ-C code 650) This. width = 650; "class =" star "src =" http://unremittingly.iteye.com/images/icon_star.png "alt =" add to favorites code "style =" border: 0px; "/>
Viewcontroller * rootviewcontroller = [[viewcontroller alloc] init];
Uinavigationcontroller * navcontroller = [uinavigationcontroller alloc]
Initwithrootviewcontroller: rootviewcontroller];
[Self. Window setrootviewcontroller: navcontroller];
5. Run the program again:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/23/32/wKiom1M02uuD8SEiAAB48M0Pmhc504.jpg "Title =" 463e196c-cd34-40c8-a886-510bf37dd1fe.png "alt =" wkiom1m02uud8seiaab48m0pmhc504.jpg "/>
As shown in the result, the position of the child view whose background color is blue is automatically moved down. The moving distance is exactly 64.0px.
Solution:
First: add a line of code in the init method of viewcontroller:
OBJ-C code 650) This. width = 650; "class =" star "src =" http://unremittingly.iteye.com/images/icon_star.png "alt =" add to favorites code "style =" border: 0px; "/>
Self. automaticallyadjustsscrollviewinsets = no;
Type 2: Make uiscrollview not the first subview of viewcontroller. Specific Operation: Modify the viewdidload Method to the following:
OBJ-C code 650) This. width = 650; "class =" star "src =" http://unremittingly.iteye.com/images/icon_star.png "alt =" add to favorites code "style =" border: 0px; "/>
-(Void) viewdidload
{
[Super viewdidload];
Uiview * firstsubview = [[uiview alloc] initwithframe: Self. View. bounds];
[Self. View addsubview: firstsubview];
Uiscrollview * scrollview = [[uiscrollview alloc] initwithframe: cgrectmake (30.0, 64.0, 260.0, 300.0)];
[Scrollview setbackgroundcolor: [uicolor redcolor];
Uiview * view = [[uiview alloc] initwithframe: scrollview. bounds];
[View setbackgroundcolor: [uicolor bluecolor];
[Scrollview addsubview: View];
[Self. View addsubview: scrollview];
}
Third: Move the sub-view of uiscorllview up to 64.0px. Modify the viewdidload method:
OBJ-C code 650) This. width = 650; "class =" star "src =" http://unremittingly.iteye.com/images/icon_star.png "alt =" add to favorites code "style =" border: 0px; "/>
Uiscrollview * scrollview = [[uiscrollview alloc] initwithframe: cgrectmake (30.0, 64.0, 260.0, 300.0)];
[Scrollview setbackgroundcolor: [uicolor redcolor];
Cgrect viewframe = cgrectmake (0,-64.0, cgrectgetwidth (scrollview. Frame ),
Cgrectgetheight (scrollview. Frame ));
Uiview * view = [[uiview alloc] initwithframe: viewframe];
[View setbackgroundcolor: [uicolor bluecolor];
[Scrollview addsubview: View];
[Self. View addsubview: scrollview];
Type 4: set transparent attributes in the navigation bar.
Self. navigationcontroller. navigationbar. translucent = Yes
Changing the transparency of the navigation bar will also affect the effect. This can be adjusted based on your actual needs. D
Uiscrollview is affected by navigation switching in the UI.