- iOS Programming Cookbook
- Hossam Ghareeb
- 367字
- 2021-07-09 18:29:49
How to do it
- As usual, let's create a new Xcode project with Single View template and name it StackViews.
- Open the storyboard file and select the view controller and change its size to iPhone 4-inch.
- From Object Library, drag a Vertical stack view and add it as a subview.
- Change its frame to (X = 20, Y = 20, Width = 280).
- We need to add constraints to make the stack view's left, right, and top margins to be equal to 20 and to make its height = 75% of the device screen. To do so, first select the stack view, and click on the Pin button and set the top, left, and right constraint values to 20:
- For the height constraint, drag the stack view (while holding the Ctrl key) to the superview and click on Equal Heights:
- Now, the stack view height will be equal to the superview height, but we need it to be 75%. So, open, selecting the stack view. Open the Size Inspector tab and double-click on the height constraint. In the multiplier value, change it to 0.75:
- You will note that there is a warning in Xcode because your stack view's frame doesn't match the constraints. To solve it, just click on the warning indicator arrow, and then click on the warning indicator triangle that will show a popup asking how you want to fix the warning. Select Update frames from the options, and click on Fix Misplacement:
- Now, your stack view is ready. Let's add some content to it. From the Object Library, drag an image view and place it inside the stack view. You will note that the image view size automatically matches the stack view size.
- Repeat the previous process for two more image views. You will note that stack view automatically lays out the three image views to match the content.
- Now, select the stack view, click on Attribute Inspector, and change its setting to match the following screenshot:
- Now, set the images for image views to any images you have or use the ones we have in the resources folder for this chapter. You should have something like this:
- Now, build and run in simulator.
推薦閱讀
- Citrix XenApp Performance Essentials
- Linux Mint Essentials
- 新手易學:系統安裝與重裝
- Ubuntu Linux操作系統
- Windows Server 2012 Hyper-V Cookbook
- Linux操作系統應用編程
- Kubernetes從入門到實踐
- 新編電腦辦公(Windows 10+ Office 2013版)從入門到精通
- Linux軟件管理平臺設計與實現
- 鴻蒙HarmonyOS手機應用開發實戰
- Android Telephony原理解析與開發指南
- Linux從入門到精通
- 計算機操作系統(第3版)(微課版)
- Linux應用大全 服務器架設
- Windows10開發入門經典