Skip to content

Instantly share code, notes, and snippets.

@monkeywithacupcake
Created October 22, 2017 06:10
Show Gist options
  • Save monkeywithacupcake/f8288e47d30dbcc0316cbd0b3dbe7db3 to your computer and use it in GitHub Desktop.
Save monkeywithacupcake/f8288e47d30dbcc0316cbd0b3dbe7db3 to your computer and use it in GitHub Desktop.
How to use a custom UIView in another View controller programmatically and handle constraints in Swift 4.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyView: UIView {
let label = UILabel()
func updateText(_ text:String?) {
guard let text = text else { return }
label.text = text
}
override init(frame: CGRect){
super.init(frame: frame)
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "MyView!"
label.numberOfLines = 0
label.textColor = .red
label.textAlignment = .center
self.addSubview(label)
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = bounds
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
} // end MyView
class MyViewController : UIViewController {
let aview = MyView()
let myview = MyView()
let parview = MyView()
override func loadView() {
let view = UIView()
view.backgroundColor = .black
self.view = view
}
override func viewDidLoad() {
setupLabels()
setupStack()
}
func setupLabels() {
aview.updateText("A View")
myview.updateText("Another View with more to say - this is a full bit of text")
parview.updateText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis porttitor interdum ex dictum auctor. Proin nec imperdiet sem. Duis pulvinar tortor viverra elit convallis tempus. Maecenas egestas elit eu convallis mattis. Sed non hendrerit tortor. Phasellus lectus dolor, pretium sed dignissim scelerisque, vulputate id dolor. Sed ac rhoncus enim.")
// can also access the label directly - but better to make a function if this will be changed often.
myview.label.textColor = .white
}
func setupStack() {
let subviews = [aview, myview, parview]
let stackView = UIStackView()
for view in subviews {
stackView.addArrangedSubview(view)
}
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)
//autolayout the stack view
let sH = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[stackView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["stackView":stackView])
let sV = NSLayoutConstraint.constraints(withVisualFormat: "V:|-30-[stackView]-30-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["stackView":stackView])
view.addConstraints(sH)
view.addConstraints(sV)
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
@monkeywithacupcake
Copy link
Author

Automatically laying out a custom view within a view controller is straightforward with storyboard, but can be frustrating without it. This Gist is a Swift 4 playground that uses a custom UIView with a centered label inside of a stackview in another view controller.
A function is in the custom view to update the text values. Different lengths of text show how the view expands.

screen shot 2017-10-22 at 15 11 13

@vijay-healthray
Copy link

But how to use reverse?

Means subview controller inside UIView.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment