import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Main View"
//Create labels programmatically with Visual Format Language (VFL) for Auto Layout
addLabels()
}
func addLabels(){
let myLabel = UILabel()
myLabel.textColor = UIColor.orange
myLabel.text = "Hello, World!"
myLabel.font = myLabel.font.withSize(16)
myLabel.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myLabel)
let myLabel2 = UILabel()
myLabel2.textColor = UIColor.red
myLabel2.font = myLabel2.font.withSize(24)
myLabel2.text = "Hello, World!"
myLabel2.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myLabel2)
var allConstraints: [NSLayoutConstraint] = []
let views = ["view": view!, "myLabel": myLabel, "myLabel2": myLabel2]
var horizontalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-(>=10)-[myLabel]-(>=10)-|", metrics: nil, views: views)
allConstraints += horizontalConstraints
horizontalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-(>=10)-[myLabel2(200)]-(>=10)-|", metrics: nil, views: views)
allConstraints += horizontalConstraints
myLabel2.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
var verticalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-(>=40)-[myLabel(50)]", metrics: nil, views: views)
allConstraints += verticalConstraints
verticalConstraints = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-(>=100)-[myLabel2]", metrics: nil, views: views)
allConstraints += verticalConstraints
NSLayoutConstraint.activate(allConstraints)
}
}
All one can think and do in a short time is to think what one already knows and to do as one has always done!
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.