Uiscrollview is affected by navigation switching in the UI.

Source: Internet
Author: User

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; "/>

  1. -(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions

  2. {

  3. Self. Window = [[uiwindow alloc] initwithframe: [uiscreen mainscreen] bounds];

  4. Self. Window. backgroundcolor = [uicolor whitecolor];

  5. // The code added for the following two actions

  6. Viewcontroller * rootviewcontroller = [[viewcontroller alloc] init];

  7. [Self. Window setrootviewcontroller: rootviewcontroller];


  8. [Self. Window makekeyandvisible];

  9. Return yes;

  10. }



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; "/>

  1. -(Void) viewdidload

  2. {

  3. [Super viewdidload];

  4. Uiscrollview * scrollview = [[uiscrollview alloc] initwithframe: cgrectmake (30.0, 64.0, 260.0, 300.0)];

  5. [Scrollview setbackgroundcolor: [uicolor redcolor];


  6. Uiview * view = [[uiview alloc] initwithframe: scrollview. bounds];

  7. [View setbackgroundcolor: [uicolor bluecolor];

  8. [Scrollview addsubview: View];


  9. [Self. View addsubview: scrollview];

  10. }


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; "/>

  1. Viewcontroller * rootviewcontroller = [[viewcontroller alloc] init];

  2. Uinavigationcontroller * navcontroller = [uinavigationcontroller alloc]

  3. Initwithrootviewcontroller: rootviewcontroller];

  4. [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; "/>

  1. 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; "/>

  1. -(Void) viewdidload

  2. {

  3. [Super viewdidload];


  4. Uiview * firstsubview = [[uiview alloc] initwithframe: Self. View. bounds];

  5. [Self. View addsubview: firstsubview];


  6. Uiscrollview * scrollview = [[uiscrollview alloc] initwithframe: cgrectmake (30.0, 64.0, 260.0, 300.0)];

  7. [Scrollview setbackgroundcolor: [uicolor redcolor];


  8. Uiview * view = [[uiview alloc] initwithframe: scrollview. bounds];

  9. [View setbackgroundcolor: [uicolor bluecolor];

  10. [Scrollview addsubview: View];


  11. [Self. View addsubview: scrollview];

  12. }


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; "/>

  1. Uiscrollview * scrollview = [[uiscrollview alloc] initwithframe: cgrectmake (30.0, 64.0, 260.0, 300.0)];

  2. [Scrollview setbackgroundcolor: [uicolor redcolor];


  3. Cgrect viewframe = cgrectmake (0,-64.0, cgrectgetwidth (scrollview. Frame ),

  4. Cgrectgetheight (scrollview. Frame ));


  5. Uiview * view = [[uiview alloc] initwithframe: viewframe];

  6. [View setbackgroundcolor: [uicolor bluecolor];

  7. [Scrollview addsubview: View];


  8. [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.

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.