In some situations there can be a need for a custom view in your iOS app. This can be a set of views, like a label and a text field or anything else that you need to use multiple times.
The steps below explain how to create a custom view in Xcode with Swift 4:
- Create a Swift file for your new UIView
- In the Project Navigator, click on “New File”
- Select “Cocoa Touch Class”
- Click “Next”
- In the “class” field fill in a proper name. (we choose: “FancyLabel”)
- In the “subclass of” field select “UIView”.
- Click “Next” and then “Create”
- Create a xib for the layout of your new custom view
- In the Project Navigator, click on “New File”
- Under the User Interface category, select “View”.
- Click “Next”
- In the “Save As” field fill in a proper name. (We choose “FancyView” again)
- Click “Create”
- Create the layout of the view
- Click on the newly create .xib file
- On the middle of your Xcode window, the visual layout appears
- Go to the “Attributes inspector” and set the value of the “Size” field to “Freeform”
- Place the controls you like.
- Link the xib to the Swift file
- Select “File’s Owner” in the editor area (left top)
- At the right, go to the “Attributes Inspector”.
- In the “Class”-field select the Swift file you created within the first set of steps (we choose: “FancyView”)
- Now change the middle UI in a way that both the xib file and the swift file are shown:
- Click on the “Show Assistant Editor”-button
- Click on the .swift file. It will appear on the left side of the editor.
- No press the “Option”-key on your keyboard and click on the .xib file. It will appear on the right side of the editor.
- Press the “Control”-key on your keyboard and keep the mouse button pressed on “View” in the xib file editor.
- Drag the appearing line to the top of the xcode file just below:
-
class FancyView: UIView { ...
- Set as name “view”. This will result in the following code:
-
class FancyView: UIView { @IBOutlet var view: UIView! /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ }
- Now add the following code so that the full code will look like this:
-
import UIKit class FancyView: UIView { @IBOutlet var view: UIView! /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } func commonInit() { Bundle.main.loadNibNamed("FancyView", owner: self, options: nil) view.fixInView(self) } } extension UIView { func fixInView(_ container: UIView!) -> Void{ self.translatesAutoresizingMaskIntoConstraints = false; self.frame = container.frame; container.addSubview(self); NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true } }
- Place your new custom view at any other view
- Go to the View or ViewController where you want to place your new custom view
- Open the view library (Command-Shift-L).
- Search for the common “UIView”.
- Place the UIView.
- Select the UIView and go to the “Identity Inspector”.
- In the field “Class” fill in the name of your custom view (in our case “FancyView”)
- You are ready. If you run the app, you will see your view appear.