iOS Code Control app page Toggle Screen2014-09-09 18:33 7683 People read Comments (1) favorite reports Classification:IOS (381)
How does iOS use the Code Control app page to toggle the screen?
This I wrote a little demo, download link http://code4app.com/ios/53c78e77933bf098108b4ea0
1. What is the first appdelegate, the Rootviewcontroller? method to be changed under:
-(BOOL) Application: (uiapplication*) Applicationdidfinishlaunchingwithoptions: (nsdictionary*) launchOptions
{ Self.window = [[UIWindow alloc]initwithframe:screenbounds];
Loadingviewcontroller * Mainviewcontroller =[[loadingviewcontroller alloc] init];
Rootnavigationcontroller * Navigationcontroller = [[Rootnavigationcontroller alloc]
Initwithrootviewcontroller:mainviewcontroller];
[Window Setrootviewcontroller:navigationcontroller];
[Self.window makekeyandvisible];
return YES;
}
Loadingviewcontroller is mine? A page, I let Loadingviewcontroller as my main page? face.
2. But until then, I can't set mainviewcontroller directly to Self.window.rootViewController
The key is that this rootnavigationcontroller *navigationcontroller ... in the middle of the turn.
The code is as follows: RootNavigationController.h? file code
#import <UIKit/UIKit.h> @interface Rootnavigationcontroller:uinavigationcontroller
@end
Rootnavigationcontroller.m? File code
#import "RootNavigationController.h" @interface Rootnavigationcontroller () @end
@implementation Rootnavigationcontroller
-(ID) Initwithnibname: (NSString *) Nibnameornilbundle: (NSBundle *) Nibbundleornil
{Self = [Super Initwithnibname:nibnameornil
Bundle:nibbundleornil]; if (self) {
Custom initialization
}
return self;}
-(void) viewdidload
{ [super viewdidload]; Do any additional setup after loading the
View.
}
-(void) didreceivememorywarning
{ [super didreceivememorywarning]; Dispose of any resources the can be
Recreated.
}
/* #pragma mark-navigation
In a storyboard-based application, you'll oftenwant to do a little preparation before navigation-(void) prepareforseg UE: (Uistoryboardsegue *) Seguesender: (ID) sender
{ //Get The new view controller using [Segue
Destinationviewcontroller]. Pass the selected object to the new view
Controller.} */-(BOOL) shouldautorotate{
return YES;}
-(Nsuinteger) supportedinterfaceorientations
{ return [self.viewControllers.lastObject
Supportedinterfaceorientations];}
-(Uiinterfaceorientation) preferredinterfaceorientationforpresentation{
return [self.viewControllers.lastObjectpreferredInterfaceOrientationForPresentation];}
@end
Rootnavigationcontroller This class is actually mediocre? No odd, the key is the next three? Method:
-(BOOL) shouldautorotate{
return YES;}
-(Nsuinteger) supportedinterfaceorientations
{ return [self.viewControllers.lastObject
Supportedinterfaceorientations];}
-(Uiinterfaceorientation) preferredinterfaceorientationforpresentation{
return [self.viewControllers.lastObjectpreferredInterfaceOrientationForPresentation];}
These three methods are used to control all the pages of the screen, the three methods are system-level;
You do not write a different set of rules; you write it, you don't call it, you run it.
The app starts off with the main function, then the main function calls Appdelegate, and Appdelegate calls Uinavigationcontroller,uinavigationcontroller in the tune is your interface.
Uinavigationcontroller inheritance is also Uiviewcontroller;
But he went further than Uiviewcontroller .
An app in general is a Uinavigationcontroller instance running, so-called Uiviewcontroller are running in this uinavigationcontroller.
And we do the view Push/pop operation, in fact, are in Navigationcontroller this inside do.
Is the only instance of the app's global Uinavigationcontroller
(Of course, do not customize this Rootnavigationcontroller class is also possible, beginners into the company interview, the interviewer is not often asked such questions, how to carry out the class extension ah? You crossing yourself to think about it, I will not repeat it here.
3. Of course, you have to use the vertical screen switch, Device Orientation, up and down four directions can not only tick One direction, of course, you need to see your project needs to support the direction, to determine, with which several points to choose which.
Then, the Uiviewcontroller in the back can be used to control the screen with the code.
4. It is also important to note that the following methods are added to the appdelegate:
-(Nsuinteger) application: (uiapplication*) application Supportedinterfaceorientationsforwindow: ( uiwindow*) window
{
IPhone doesn ' t support upside down by default while the IPad does. Override to allow all orientations always, and let the root view controller decide "s allowed (the supported Orientati ONS mask gets intersected).
returnuiinterfaceorientationmaskall;
}
5. The following page controls the leveling method:
Simply add the following method directly, of course, this is a forced vertical screen example
-(Nsuinteger) supportedinterfaceorientations{
returnuiinterfaceorientationmaskportrait;
}
-(BOOL) shouldautorotate
{
returnNO;
}
-(uiinterfaceorientation) preferredinterfaceorientationforpresentation
{
returnuiinterfaceorientationportrait;
}
The meaning of each method and left and right I do not explain in detail, to check the document, say directly directly command+ left-hand can also see the file bar, inside the note is also very clear.
This is an example of a forced right turn.
-(BOOL) shouldautorotate
{
returnNO;
}
-(uiinterfaceorientation) preferredinterfaceorientationforpresentation
{
returnuiinterfaceorientationlandscaperight;
}
-(Nsuinteger) supportedinterfaceorientations
{
returnuiinterfaceorientationmasklandscaperight;
}
Of course,shouldautorotate is used to control auto-rotation, and you can set
6. In addition, it is important to switch from a (vertical screen) to B (horizontal screen), can not use apushviewcontrollerb,
Must use a Presentviewcontroller B.
[Self.navigationcontroller pushviewcontroller:b Animated:yes]; (X)
[Self presentviewcontroller:b animated:yes completion:nil]; (V)
You know what the problem is.
Sample demo, download link http://code4app.com/ios/53c78e77933bf098108b4ea0
Force a page to screen vertically