官术网_书友最值得收藏!

How to do it...

  1. Go to Xcode and create a new iOS project with template Single View Application. Set the name of the project to UIViews.
  2. Now, select the storyboard file and open the single page view controller:

  1. In the Attribute Inspector in the right-hand side bar, set the size of the View Controller to iPhone 4.7-inch size.
  2. 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).
  1. 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:

  1. 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:

  1. 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)
  1. Now, build and run the project; you should see something like this:

主站蜘蛛池模板: 保定市| 姚安县| 辽阳县| 卓资县| 西贡区| 太和县| 文化| 孝感市| 高雄县| 伊金霍洛旗| 彝良县| 克山县| 滨州市| 北海市| 八宿县| 东丽区| 集贤县| 浮山县| 张家口市| 乐安县| 永和县| 油尖旺区| 宜兰县| 大关县| 云和县| 南京市| 平乡县| 五华县| 旬邑县| 绵竹市| 十堰市| 梅河口市| 紫阳县| 阳春市| 灌云县| 宜川县| 建始县| 石景山区| 句容市| 财经| 桐庐县|