- iOS Programming Cookbook
- Hossam Ghareeb
- 280字
- 2021-07-09 18:29:47
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:
推薦閱讀
- 30天自制操作系統(tǒng)
- Linux網(wǎng)絡(luò)管理與配置(第2版)
- Windows Server 2019 Cookbook
- Linux系統(tǒng)架構(gòu)與運(yùn)維實(shí)戰(zhàn)
- UNIX操作系統(tǒng)設(shè)計(jì)
- Kali Linux滲透測(cè)試全流程詳解
- vSphere Virtual Machine Management
- PLC控制系統(tǒng)應(yīng)用與維護(hù)
- Windows Phone應(yīng)用程序開(kāi)發(fā)
- Kubernetes從入門(mén)到實(shí)踐
- 數(shù)據(jù)中心系統(tǒng)工程及應(yīng)用
- Dreamweaver CS5.5 Mobile and Web Development with HTML5,CSS3,and jQuery
- 注冊(cè)表應(yīng)用完全DIY
- OpenSolaris設(shè)備驅(qū)動(dòng)原理與開(kāi)發(fā)
- Django Project Blueprints