This article will look at a few code examples of creating objects in Swift 3 without using the Interface Builder. Whilst the IB in recent versions of Xcode has become more reliable and less buggy, many developers still wish to learn the code behind creating the view elements.
In addition to the Creating Elements using Code post, we look at some further elements below.
The objects can be created by adding code in the viewDidLoad
function in the ViewController.swift
file.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.text = "This is a test label"
label.textAlignment = .center
self.view.addSubview(label)
}
override func viewDidLoad() {
super.viewDidLoad()
....
// Button
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 150, height: 44))
button.center = CGPoint(x: 160, y: 200)
button.setTitle("Click Me!", for: .normal)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
}
With the UIButton
code, the addTarget
function requires an action function to also be defined.
In the code example above, a buttonAction
function should be defined.
func buttonAction(){
//...
}
override func viewDidLoad() {
super.viewDidLoad()
....
let switchDemo = UISwitch(frame: CGRect(x: 150, y: 300, width: 0, height: 0))
switchDemo.isOn = true
switchDemo.setOn(true, animated: false)
switchDemo.addTarget(self, action: #selector(switchChange), for: .valueChanged)
self.view.addSubview(switchDemo)
}
func switchChange() {
//...
}
override func viewDidLoad() {
super.viewDidLoad()
....
let alertController = UIAlertController(title: "Alert", message: "Here is an alert", preferredStyle: UIAlertControllerStyle.alert)
let destructiveAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive) {
(result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
}
alertController.addAction(destructiveAction)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
....
let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
self.view.addSubview(navBar);
}
override func viewDidLoad() {
super.viewDidLoad()
....
let navItem = UINavigationItem(title: "SomeTitle");
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: #selector(menuItemAction));
navItem.rightBarButtonItem = doneItem;
navBar.setItems([navItem], animated: false);
}
func menuItemAction() {
//...
}
override func viewDidLoad() {
super.viewDidLoad()
....
let sliderDemo = UISlider(frame:CGRect(x: 20, y: 260, width: 280, height: 20))
sliderDemo.minimumValue = 0
sliderDemo.maximumValue = 100
sliderDemo.isContinuous = true
sliderDemo.tintColor = UIColor.red
sliderDemo.value = 50
sliderDemo.addTarget(self, action: #selector(sliderValueDidChange), for: .valueChanged)
self.view.addSubview(sliderDemo)
}
func sliderValueDidChange() {
//...
}
Note: This article is based on iOS 10 and Swift version 3.