Rotation of the screen in iOS (uiviewcontroller) Horizontal screen vertical screen

Source: Internet
Author: User
Tags uikit

Rootviewcontroller

//View Controller (Uiviewcontroller): It is not a view, it is used to manage views, so it is not visible on the screen, but it carries a view (root view)#import"RootViewController.h"#import"LoginView.h"//extension of the View controller@interface Rootviewcontroller () @end//implementation part of the view controller@implementation Rootviewcontroller//used to load the view controller's root view, this method originates from the parent class- (void) loadview{LoginView*logview =[[LoginView alloc] initwithframe:[[uiscreen mainscreen] bounds]; //Specifies that the custom view object you just created is the root view of the current View controllerSelf.view =LogView; [LogView release];}//This method is the method that executes immediately after the Loadview executes//call Loadview to load the root view when access to view controller is empty, one but the discovery view is empty//If you want to customize the view controller's root view, you only need to override the Loadview method, complete the creation in the Loadview method, and specify the root view of the current attempt controller to- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.NSLog (@"%s,%d", __function__,__line__); //Self.view.backgroundColor = [Uicolor redcolor];    }#pragmaMark-detects and handles the rotation of the screen//1. Set the rotation direction supported by the screen-(Nsuinteger) supportedinterfaceorientations{returnUiinterfaceorientationmaskall; }//the method that will trigger when the rotation starts//often used to pause player playback, pause video playback, and turn off user interaction- (void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (NSTimeInterval) duration{[Super Willrotatetointerfaceorientation:tointerfaceorientation duration:duration];}//triggered when the rotation is over//continue music, video playback, and open user interaction- (void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation{[super Didrotatefrominterfaceorientation:frominterfaceorientation];}//triggered when an animation is about to start spinning, often used to add custom animations when rotated- (void) Willanimaterotationtointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: ( Nstimeinterval) duration{[Super Willanimaterotationtointerfaceorientation:tointerfaceorientation Duration:d            Uration]; }#pragmaWays to control Layout views---view Controller//when the view is rotated, the view controller triggers the method used to re-layout the view controller according to the view's child view- (void) viewwilllayoutsubviews{[Super Viewwilllayoutsubviews];}//triggered when a memory warning is received by the view Controller//frees previously unused space, as well as objects that can be rebuilt.- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated. //1. The view controller's root view is sufficient to have successfully loaded//2. Whether the current root view is being displayed//[self isviewloaded] Determines whether the view is loaded successfully//Self.view.window, interpreting whether the view is in the current interface//the memory is loaded successfully and is not in the current interface    if([self isviewloaded] &&!Self.view.window) {[Self.view release]; }} @end

LoginView.h

#import <UIKit/UIKit.h>@interface loginview:uiview// simplest Custom view encapsulation: Declare a control directly as a property, Declare an external access interface in. h . @property (nonatomic,retain) UILabel ** * *loginbtn; @end

loginview.m

#import"LoginView.h"#defineKmargin_left_label 30#defineKmargin_top_label 100#defineKwidth_label 60#defineKheight_label 30#defineKmargin_left_textfield (Kmargin_left_label + Kwidth_label + 20)#defineKmargin_top_textfield 100#defineKwidth_textfield 150#defineKheight_textfield 30#defineKmargin_left_button 100#defineKmargin_top_button (Kmargin_top_label + Kheight_label + 30)#defineKwidth_button 80#defineKheight_button 40@implementation LoginView//principle: If you override the parent class inheritance method, call the parent class's implementation (super) If you do not know what the parent class does with the method.-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {[self setuplabel];                [Self Setupbutton];    [Self Setuptextfield]; }    returnSelf ;}//User name label- (void) setuplabel{Self.alabel=[[UILabel alloc] init]; Self.aLabel.text=@"User name"; Self.aLabel.frame=CGRectMake (Kmargin_left_label,kmargin_top_label, Kwidth_label, Kheight_label);    [Self AddSubview:self.aLabel]; [Self.alabel release];}//Input Box- (void) setuptextfield{Self.textfield=[[Uitextfield alloc] init]; Self.textField.placeholder=@"Please enter user name"; Self.textField.borderStyle=Uitextborderstyleroundedrect; Self.textField.frame=CGRectMake (Kmargin_left_textfield, Kmargin_top_textfield, Kwidth_textfield, Kheight_textfield);    [Self AddSubview:self.textField]; [Self.textfield release];}//Login Button- (void) setupbutton{self.loginbtn=[UIButton Buttonwithtype:uibuttontypesystem]; [Self.loginbtn settitle:@"Login"Forstate:uicontrolstatenormal]; Self.loginBtn.frame=CGRectMake (Kmargin_left_button, Kmargin_top_button, Kwidth_button, Kheight_button); [Self addSubview:self.loginBtn];}//The method is the method of the view, and when rotated, the view automatically calls the method to re-layout the child view.- (void) layoutsubviews{[Super Layoutsubviews]; //1. Get the status of the current screen (rotation direction)Uiinterfaceorientation Orientation =[UIApplication sharedapplication].statusbarorientation; //2. Determine the screen orientation, adjust the sub-view    Switch(orientation) { Caseuiinterfaceorientationportrait://NSLog (@ "vertical screen"); //Break ;                          CaseUiinterfaceorientationportraitupsidedown://NSLog (@ "Upside down");Self.loginBtn.frame =CGRectMake (Kmargin_left_button, Kmargin_top_button, Kwidth_button, Kheight_button);  Break;  CaseUiinterfaceorientationlandscapeleft://NSLog (@ "Right horizontal screen"); //Break ;                CaseUiinterfaceorientationlandscaperight://NSLog (@ "Zoo");Self.loginBtn.frame = CGRectMake (Kmargin_left_textfield + Kwidth_textfield + -, Kmargin_top_label, Kwidth_button, Kheight_button);  Break; default:             Break; }}- (void) dealloc{[_alabel release];    [_textfield release];    [_LOGINBTN release];    [Super Dealloc]; } @end

Rotation of the screen in iOS (uiviewcontroller) Horizontal screen vertical screen

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.