02.The Window Supplement--experimenting

Source: Internet
Author: User

Experimenting with views

Pmst
Article: Views-experimenting with views
Series: The Swift Beginner
Written on: 2015.04.26

Personal blog through train
Team blog through train

Reviewing the previous article, we discussed in detail how to create a window project using the a main storyboard and the pure code two cases. The interface start-up,window creation, configuration to the interface rendering process is described in general. This chapter complements the Window to explain how to add a view controller and view operations.

Use of Storyboard

First, open Xcode, create a new single View application project, andXcode will automatically generate a main.storyboard file to include in the project Next, select Click to view Storyboard content, default is a scene, including a view controller, and the controller contains when the app starts, as the view controller described earlier becomes the Rootviewcontrollerof the Application main window (if you don't, review the above), View controller views become the root viewof the main window.

You can try dragging a view from the Object library (that is, the third icon in the lower-right corner) to main view as its subView , when the app runs, The dragged-in view is automatically instantiated, and of course you can choose to instantiate a view with code and then add it to main view . Usually we choose to add the above code configuration in the viewdidload method in the View controller and use Self.view to get the main view for the view controller. Just like the following code:

override func viewDidLoad(){    super.viewDidLoad()    self.view    UIView(frame:CGRectMake(100,100,50,50))    v.backgroundColorUIColor.redColor//红色的小正方形    mainview.addSubView(v)                  //添加到主视图中}
Creating with Code

Well... We need an empty project (as previously described, click here), no . xib file, no . Storyboard file, completely dependent on the code to achieve the results of the previous demo . For beginners, this is certainly more exciting!

As shown in the last demoin the window , because there is no Rootviewcontrollerassigned to the window by a view controller, a warning is generated when the app starts. The simplest solution is to application:didFinishLauchingWithOptions: create a view controller in the method and appoint it as the rootviewcontrollerof window . The code is as follows:

Func Application (Application:uiapplication, Didfinishlaunchingwithoptions launchoptions: [NSObject: Anyobject]?) -Bool { Self. Window=UIWindow(Frame:uiscreen. Mainscreen(). Bounds) Self. Window. Rootviewcontroller=Uiviewcontroller()//This is where a new view controller is created.Let MainView = Self. Window!. Rootviewcontroller!. View  //Get the main viewLet V =UIView(Frame:cgrectmake ( -, -, -, -))//Customize a viewV. BackgroundColor=Uicolor. Redcolor()//Set color to redMainview. Addsubview(v)//Add custom red views to the main view     Self. Window!. BackgroundColor=Uicolor. Whitecolor() Self. Window!. Makekeyandvisible()//This step is important    return true}

02.The Window Supplement--experimenting

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.