There are two ways to write iOS application UI, one is Storyboard/Xib, and the other is hand-written code. Storyboard/Xib organizes UI. Because it provides visualization features, you can display the results by dragging UI controls from the UI library, which greatly improves the development speed. But there is a problem with multi-person collaborative development. Since all UIs are placed in the same Storyboard file, conflicts will occur when merging codes using Git/SVN. Multi-person collaborative development is not the main problem. Some people have proposed to create multiple Storyboards to separate UI writing. The main problem of Storyboard/Xib is that the code reusability is relatively poor. So some people choose to hand-write UI code, which not only solves the problem of multi-person collaborative development, but also uses custom controls in multiple Views. But every time you write UI code by hand, you have to compile, build and run it, and finally display it in the simulator, which will slow down the development speed. If you save the changes every time you modify the UI control, the modified results will be displayed in the simulator in real time, which can greatly improve the speed of writing UI. Auto Layout What is Auto Layout Auto Layout is a constraint-based layout system that adjusts the position and size of UI elements according to the constraint relationship between UI elements. What problems does Auto Layout solve?
How to use Auto Layout The constraint class in Auto Layout corresponds to NSLayoutConstraint, and there are two main ways to create NSLayoutConstraint objects. The first is
The main meaning of the above method is that the attribute1 of a view1 is equal to (less than or equal to/greater than or equal to) the multiplier of the attribute2 of a view2 plus the constant. The attribute mainly consists of the following values representing the position (up/down/left/right) and size (width/height):
Simplified, the formula can be expressed as:
The second way is:
This method mainly uses Visual Format Language to describe constraint layout. Although the syntax is relatively concise, it is less readable and prone to errors. Problems with Auto Layout Although Auto Layout is very powerful and flexible in laying out views, the syntax for creating constraints is too complicated. Here is an example from Masonry:
If you need to write so many lines of code for such a simple example, imagine how painful it would be to create constraints for multiple views. Another way is to use Visual Format Language (VFL), which has a simpler syntax but poor readability and is prone to errors. Masonry Why use Masonry Masonry uses a chained DSL (Domain-specific language) to encapsulate NSLayoutConstraint. This makes Auto Layout layout code easier to read and concise.
Even shorter
#p# How to use There are three ways to use Masonry to create constraints to define layouts: mas_makeConstraints, mas_updateConstraints, and mas_remakeConstraints. 1. mas_makeConstraints After creating a constraint using mas_makeConstraints, you can use a local variable or attribute to save it for next reference; if you create multiple constraints, you can use an array to save them.
2. mas_updateConstraints Sometimes you need to update constraints (for example, animation and debugging) instead of creating fixed constraints. You can use the mas_updateConstraints method.
3. mas_remakeConstraints mas_remakeConstraints is similar to mas_updateConstraints, both of which update constraints. However, mas_remakeConstraints deletes the previous constraint and then adds a new constraint (suitable for moving animations); while mas_updateConstraints only updates the value of the constraint.
To learn more about the above three code snippets, you can download the Masonry iOS Examples project for reference. Classy Classy Introduction and Features Classy is a stylesheet system that can be seamlessly integrated with UIKit. It borrows the ideas of CSS, but introduces new syntax and naming rules.
You can use { } : ; to limit the stylesheet
Or you can use spaces to delimit the stylesheet
Default Style Classy looks for a stylesheet file named stylesheet.cas in the application bundle by default. If you use this file name, you can load the stylesheet file without doing anything.
If you also want to get error information for debugging when an error occurs, you can use -(void)setFilePath:error:
If you use Storyboard/Xib to organize the UI interface, you need to set the filePath in the int main(int argc, char * argv[]) method of main.m to ensure that the stylesheet is loaded before the UIWindow is created. Otherwise (using handwritten UI code), you set the filePath in the - (BOOL)application:didFinishLaunchingWithOptions: method of AppDelegate.m Live Reload Live Reload is a key feature that displays the effects of writing UI code in real time. It can check stylesheet file changes in real time without recompiling, building, and running the simulator, thus greatly improving development speed.
Selectors Style Selectors are a way to specify which view uses which style. There are three main ways to specify the target view:
You can mix and match the three methods, as shown below:
For detailed usage, please refer to the Selectors section of the official website.
Properties Classy supports all UIAppearance properties and methods, as well as many properties that are not related to UIAppearance. Classy uses the same property naming as UIKit, so you don't have to think about how to map style properties to Objective-C properties.
The name of the style property uses the same name as Objective-C
The naming convention for style properties is kebab case.
For detailed usage, please refer to the Properties section of the official website. Keep it DRY(Don't Repeat Yourself) A very important principle in programming is to avoid duplication, which can not only greatly reduce duplicate code, but also make the code easier to reuse and maintain. Classy provides three ways to avoid code duplication: grouping, nesting, variables Grouping If there are two or more style selectors sharing the same attribute
We can extract the same attributes into grouped style selectors
#p# Nesting If two or more style selectors share the same view hierarchy
We express the view hierarchies in this way through nesting
Variables Classy allows you to store multiple identical style property values for sharing by defining variables. The variable naming rules are as follows:
ClassyLiveLayout ClassyLiveLayout combines Classy stylesheets with Masonry to create a tool that allows you to fine-tune Auto Layout constraints in real time in a running simulator. ClassyLiveLayout has a core category: UIView+ClassyLayoutProperties, which defines the following properties in UIView:
cas_margin and cas_size represent the position and size of UI elements respectively, while the rest of the properties are further subdivisions of the two properties. We can access style properties from stylesheets to define constraint layouts, separating data from code, which is conducive to modifying and reusing code.
We can reference style properties when defining layouts in updateConstraints or updateViewConstrains
When defining view layouts, Auto Layout constraints are placed in stylesheets and live reloaded. If you modify constraints, you can see the effect of the changes in real time without having to recompile, build, and run the simulator. Sample Project Configuration Project Since Masonry, Classy and ClassyLiveLayout need to be referenced, the Podfile is configured as follows:
Writing Code 1. Add stylesheet.cas file to the project After installing Masonry, Classy and ClassyLiveLayout, the first time you run the project, you will see an error message saying there is no stylesheet.cas file: No stylesheet.cas file error.png Just add an empty stylesheet.cas file to your project. Create stylesheet.cas file.png 2. Create a LiveView class that inherits SHPAbstractView. Create LiveView inherit SHPAbstractView.png Create a LiveView object in ViewController and then reference it by self.view. Setup root view in ViewController.png When compiling and running, a compilation error occurs in SHPAbstractView.h because UIView cannot be found. SHPAbstractView Compile error.png #p# The problem can be solved by simply introducing UIKit, but when running the application, the following error occurs: Must override methods.png The main reason is that any custom UIView that inherits SHPAbstractView needs to override two methods: - (void)addSubviews and - (void)defineLayout. We can see from the source code of SHPAbstractView: SHPAbstractView Source Code .png So just override two methods in the LiveView.m file
3. LiveView class design LiveView mainly consists of two properties: redBoxView and blueBoxView. redBoxView represents the red square, and blueBoxView represents the blue square.
4. LiveView class implementation Since the SHPAbstractView class has already handled how to initialize the View, it exposes two interfaces - (void)addSubviews and -(void)defineLayout to handle building the view hierarchy and defining the layout respectively. The subclass only needs to override these two methods of SHPAbstractView to create a LiveView.
Once you have the constraint data, you can lay it out in code:
5. The simulator supports Live Reload To enable Live Reload, you need to specify the stylesheet path and only run on the simulator. Support Live Reload.png The effect at this time: #p# 6. Separate style files Because some netizens have raised the following question: If the styles of all views are placed in the same stylesheet.cas file, the stylesheet.cas file will be complicated, and it will not be easy to merge the code when multiple people collaborate on development. Therefore, it is necessary to separate the style files into multiple files.
stylesheet.cas file.png ***Effect Sample code storage address: LiveAutoLayout Summarize Before, every time you change the handwritten UI code, you usually have to recompile, build, and run the simulator to see the effect. But after using Masonry, Classy, and ClassLiveLayout together, you can say goodbye to this time-consuming process and greatly improve the development speed. In addition, we put all Auto Layout constraints in stylesheets for real-time loading (Live reload), separate layout data from layout code, and make the code more reusable and maintainable. Classy also provides three ways to avoid duplication: Grouping, Nesting, and Variable, so that style data can be reused as much as possible. |
<<: Runtime series (analysis of data structure)
>>: Cocos Play: The best solution for mobile web games
Davy Zhai is a senior foreign trade person in cro...
1. Should the thyroid gland be removed if a thyro...
WeChat has begun to crack down on third-party web...
Chickens are the most widely distributed and most...
In view of the fact that everyone had many diffic...
According to foreign media reports, workers at BM...
Reviewer: Chen Mingyong, Professor of the School ...
Invitation is one of the ways to achieve product ...
In 2000, a levitating frog won the Ig Nobel Prize...
The blind box circle doesn’t seem to be peaceful ...
It’s the Lantern Festival again. Have you thought...
The May Day holiday is over. Many people enjoyed ...
2019 is a year full of uncertainty. The global ec...
The latest SEO training practice case: Electric s...
The content of SEM bidding hosting provided by Ku...