用干净的代码创建ios ui

    科技2025-03-07  50

    During the last months, I’ve been working on projects where the UI had to be created with code. Xib files or Storyboards were not used, and UIKit framework was applied instead of SwiftUI framework.

    在过去的几个月中,我一直在从事必须使用代码创建UI的项目。 没有使用Xib文件或Storyboard,而是应用了UIKit框架而不是SwiftUI框架。

    Sometimes it’s painful to create the UI with code because we cannot see what we are building until we run the App on a device or on an iOS Simulator. Another main problem is the amount of code we have to write: create controls, set them up, add constraints, etc. The code could turn out a disaster if we are not neat.

    有时使用代码创建UI会很痛苦,因为只有在设备或iOS模拟器上运行应用程序之后,我们才能看到正在构建的内容。 另一个主要问题是我们必须编写的代码量:创建控件,设置它们,添加约束等。如果代码不够整洁,这些代码可能会带来灾难。

    In this post, I’m going to show you how we can achieve clean code to create the UI with the UIKit framework.

    在这篇文章中,我将向您展示如何使用UIKit框架实现干净的代码来创建UI。

    我们要做什么? (What we are going to do?)

    We are going to build a small app with some credit cards.

    我们将使用一些信用卡来构建一个小型应用程序。

    The UIStackView is going to be our main component to create the UI in an easy way and we are going to use two Pods to make the code beautiful:

    UIStackView将成为我们以简单方式创建UI的主要组件,并且我们将使用两个Pod使代码漂亮:

    Then (https://github.com/devxoul/Then)

    然后(https://github.com/devxoul/然后)

    Super sweet syntactic sugar for Swift initializers.

    Swift初始化程序的超级甜蜜语法糖。

    Anchorage (https://github.com/Rightpoint/Anchorage)

    安克雷奇(https://github.com/Rightpoint/安克雷奇)

    A lightweight collection of intuitive operators and utilities that simplify Auto Layout code.

    直观的操作员和实用程序的轻量级集合,简化了自动布局的代码。

    Let’s get started!

    让我们开始吧!

    添加豆荚 (Adding Pods)

    First of all, we need to add the pods to our Podfile.

    首先,我们需要将Pod添加到Podfile中。

    Then we have to install them using our Terminal with the command pod install. Remember to be in the same location as the Podfile.

    然后,我们必须使用带有pod pod install命令的Terminal来安装它们。 请记住与Podfile位于同一位置。

    UI结构 (UI Structure)

    To better understand the structure that we are going to code, I have made a small drawing:

    为了更好地理解我们将要编码的结构,我制作了一个小图:

    编码时间! (Coding Time!)

    So, let’s create the first method which is going to contain our two main components: the UIScrollView and the vertical UIStackView.

    因此,让我们创建第一个方法,该方法将包含我们的两个主要组件:UIScrollView和垂直UIStackView。

    Note: Remember to import Anchorage and Then frameworks.

    注意:请记住要导入Anchorage和Then框架。

    Let me explain this code a little bit.

    让我解释一下这段代码。

    In the first line, I added layout margins of 16 px to the Root View. So, all the components within it will have those margins and we won’t have to add 16px to each component.

    在第一行中,我向“根视图”添加了16像素的布局边距。 因此,其中的所有组件都将具有这些边距,而我们不必在每个组件上添加16px。

    To give the same inset to all the margins, I used this small extension:

    为了使所有页边距具有相同的插图,我使用了以下扩展名:

    Then I created the UIScrollView, I added it to the Root View and I used Anchorage to give it constraints to the edges of the margins guide of the Root View.

    然后创建UIScrollView,将其添加到Root View中,并使用Anchorage将其约束赋予Root View边距指南的边缘。

    After that I’ve created the main vertical UIStackView using Then framework to access the UIStackView’s properties (I wanted to add space of 16px between all the components within it), in a beautiful way and using the following UIStackView extension to give it the ability to choose axis and pass the arranged subviews in the Init method:

    在那之后,我创建了一个主要的垂直UIStackView,然后使用Then框架来访问UIStackView的属性(我想在其中的所有组件之间添加16px的空间),并使用以下UIStackView扩展使其具有以下功能:选择轴并在Init方法中传递排列的子视图:

    This UIStackView is going to hold the credit cards (we will leave it empty so far). So, I’ve added it as subview of the UIScrollView and I have incorporated edge constraints and a width constraint using Anchorage, super snappy!

    这个UIStackView将持有信用卡(到目前为止,我们将其留空)。 因此,我将其添加为UIScrollView的子视图,并使用Anchorage(超级快活)结合了边缘约束和宽度约束!

    So far, so good!

    到现在为止还挺好!

    Let’s create a Struct for our card information called CardInfo. This is going to contain all the information displayed within the card, like the card holder, the card number, the balance and the expiration date.

    让我们为名为CardInfo的卡信息创建一个Struct。 这将包含卡内显示的所有信息,例如持卡人,卡号,余额和有效期。

    Now we can create three instances of CardInfo like this:

    现在,我们可以创建CardCard的三个实例,如下所示:

    We’re going to use these instances in a second, but first, we need to create a method for the card creation.

    我们将在一秒钟内使用这些实例,但是首先,我们需要创建一种创建卡的方法。

    Remember the card is built with this structure:

    请记住,该卡是使用以下结构构建的:

    A UIView as background.

    一个UIView作为背景。 A vertical UIStackView as main container. It contains two horizontal UIStackViews (one for the card image and the balance, and another one to hold two more vertical UIStackViews), and a label for the card number.

    垂直的UIStackView作为主容器。 它包含两个水平的UIStackView(一个用于卡片图像和余额,另一个用于容纳两个垂直的UIStackView)和一个卡号标签。 The two horizontal UIStackViews contain four UILabels. On the left side two labels for the card holder and on the right side two labels for the expiration date.

    两个水平的UIStackViews包含四个UILabel。 左侧有两个标签,分别代表持卡人,右侧有两个标签,表明有效期。

    This method receives as parameter the card information that we want to show and the card color.

    此方法接收我们要显示的卡片信息和卡片颜色作为参数。

    First I added a UIView as background because we cannot change the background color and the corner radius of an UIStackView. So, I did the trick with this UIView. Again, I used layout margins of 16px to keep all the card components in place.

    首先,我添加了UIView作为背景,因为我们无法更改UIStackView的背景颜色和拐角半径。 因此,我使用此UIView做到了。 同样,我使用16px的布局边距将所有卡组件保持在原位。

    Note that I used Then framework for all the Init methods of the components.

    请注意,我对组件的所有Init方法都使用了Then框架。

    Then I created the structure for the information with the UIStackViews (some of them empties so far) and giving them the spacing that I want between the components, the alignment, and the distribution. There are a couple of options for the distribution, so I recommend you take a look at this link and choose the best one for your case.

    然后,我使用UIStackViews(到目前为止有些是空的)创建了信息的结构,并为它们提供了组件,路线和分布之间所需的间距。 分发有两个选项,所以我建议您看一下此链接,然后为您的情况选择最佳的链接。

    I’ve also added the UILabel for the card number.

    我还为卡号添加了UILabel。

    After that, I incorporated the main vertical UIStackView as subview of the background UIView and I added the edge constraints to its layout margins guide. Also I called layoutIfNeeded method to update the constraints and I returned the card view.

    之后,我将主要的垂直UIStackView合并为背景UIView的子视图,并将边缘约束添加到其布局边距指南中。 我还调用了layoutIfNeeded方法来更新约束,并返回了卡片视图。

    Now with the base constructed, let’s add the card information.

    现在,随着基础的构建,让我们添加卡信息。

    The UILabels are similar in styling, so we can create a method to reuse code.

    UILabel的样式相似,因此我们可以创建一种方法来重用代码。

    Here I’ve created a UILabel with the parameters given, pretty straight forward.

    在这里,我使用给定的参数创建了一个UILabel,非常简单。

    In the createCard method, I added the UIImageView for the card icon and the UILabels.

    在createCard方法中,我为卡图标和UILabels添加了UIImageView。

    Obviously, we can refactor the two UILabels to use just one with attributed strings. But the goal is to show the use of UIStackViews with multiple components.

    显然,我们可以重构两个UILabel以仅使用一个带有属性字符串的UILabel。 但目标是展示如何将UIStackViews与多个组件一起使用。

    Finally, I called createCard method within our main UIStackView on setupCreditCardUI and I called this last one on the viewDidLoad method.

    最后,我对我们的主要setupCreditCardUI内UIStackView称为createCard方法,我呼吁viewDidLoad方法这最后一个。

    That’s it!

    而已!

    You can see the complete code here.

    您可以在此处查看完整的代码。

    结论 (Conclusion)

    When using UIStackViews we can avoid a lot of code related to auto-layout, we can set the spacing and distribution between the components to keep them in place and we can get blocks of code that help us to organize it and get a clean UI file.

    使用UIStackViews时,我们可以避免使用大量与自动布局有关的代码,可以设置组件之间的间距和分布以将它们固定在适当的位置,并且可以获取代码块来帮助我们组织和获取干净的UI文件。

    Using Then can help us to improve the code if we have to set a lot of properties making it as a block of code.

    如果必须设置很多属性使其成为代码块,那么使用Then可以帮助我们改进代码。

    If we use Anchorage we get a super easy way to set some constraints when they are needed.

    如果使用Anchorage,我们将获得一种在需要时设置某些约束的超级简单方法。

    I’ve been using a combination of these three techniques for a couple of months and it helped me a lot when I have to create all the UI with code. It’s easy to determine where is each component created and easy to do UI changes.

    我使用这三种技术的组合已经有几个月了,当我必须使用代码创建所有UI时,这对我有很大帮助。 可以轻松确定每个组件的创建位置,并且可以轻松更改UI。

    Now it’s your turn!

    现在轮到你了!

    翻译自: https://medium.com/@luayzaemanuel/creating-ios-ui-with-clean-code-932eb1351e05

    Processed: 0.021, SQL: 8