上QQ阅读APP看书,第一时间看更新
How to do it...
- Go to Xcode and create a new iOS project with template Single View Application. Set the name of the project to UIViews.
- Now, select the storyboard file and open the single page view controller:
- In the Attribute Inspector in the right-hand side bar, set the size of the View Controller to iPhone 4.7-inch size.
- Open the View Controller view, and from Object Library in the right-hand side bar, drag two UIViews. In the first UIView and from the Size Inspector tab in the right side bar, set the frame of the first one to (x = 0, y = 0, width = 375, height = 300), and from the Attribute Inspector tab, set red as the background color. In the second UIView, set the frame to (x = 0, y = 300, width = 375, height = 300).
- Rename the two views by selecting each one and hit the Enter key. The title will be converted to a text field where you can rename your views. Change the first view to RedView and the second one to BlueView. You should see something like this:
- Take an outlet for your views. Click on Assistant Editor at the top bar to open the ViewController.swift file, and drag the view to the source code file to add outlets by selecting each view and holding the Ctrl key:
- Now, open the ViewController.swift file in the viewDidLoad function and add the following code:
let yellowView = UIView(frame: CGRectMake(0, 0, 200, 100))
yellowView.backgroundColor = UIColor.yellowColor()
self.redView.addSubview(yellowView)
let brownView = UIView(frame: CGRectMake(100, 50, 200, 100))
brownView.backgroundColor = UIColor.brownColor()
self.redView.insertSubview(brownView, belowSubview: yellowView)
- Now, build and run the project; you should see something like this: