Created
August 4, 2020 10:06
-
-
Save anoop4real/c32b872efbe2f718e1f19b886a17d1c9 to your computer and use it in GitHub Desktop.
A sample code to show custom width/ height proportion in stackview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
class MyViewController : UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
self.view = view | |
setUpView() | |
//setUpViewVertical() | |
} | |
private func setUpView() { | |
let viewStack = createAStackViewWith(axis: .horizontal) | |
let view1 = createAUIView(with: .red) | |
let view2 = createAUIView(with: .green) | |
let view3 = createAUIView(with: .blue) | |
view.addSubview(viewStack) | |
viewStack.addArrangedSubview(view1) | |
viewStack.addArrangedSubview(view2) | |
viewStack.addArrangedSubview(view3) | |
NSLayoutConstraint.activate([ | |
viewStack.leftAnchor.constraint(equalTo: view.leftAnchor), | |
viewStack.rightAnchor.constraint(equalTo: view.rightAnchor), | |
viewStack.topAnchor.constraint(equalTo: view.topAnchor, constant: 44), | |
viewStack.heightAnchor.constraint(equalToConstant: 44), | |
view1.widthAnchor.constraint(equalTo: viewStack.widthAnchor, multiplier: 0.50), | |
view2.widthAnchor.constraint(equalTo: viewStack.widthAnchor, multiplier: 0.30), | |
view3.widthAnchor.constraint(equalTo: viewStack.widthAnchor, multiplier: 0.20), | |
]) | |
} | |
private func setUpViewVertical() { | |
let viewStack = createAStackViewWith(axis: .vertical) | |
let view1 = createAUIView(with: .red) | |
let view2 = createAUIView(with: .green) | |
let view3 = createAUIView(with: .blue) | |
view.addSubview(viewStack) | |
viewStack.addArrangedSubview(view1) | |
viewStack.addArrangedSubview(view2) | |
viewStack.addArrangedSubview(view3) | |
NSLayoutConstraint.activate([ | |
viewStack.leftAnchor.constraint(equalTo: view.leftAnchor), | |
viewStack.rightAnchor.constraint(equalTo: view.rightAnchor), | |
viewStack.topAnchor.constraint(equalTo: view.topAnchor), | |
viewStack.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
view1.heightAnchor.constraint(equalTo: viewStack.heightAnchor, multiplier: 0.50), | |
view2.heightAnchor.constraint(equalTo: viewStack.heightAnchor, multiplier: 0.30), | |
view3.heightAnchor.constraint(equalTo: viewStack.heightAnchor, multiplier: 0.20), | |
]) | |
} | |
private func createAStackViewWith(axis: NSLayoutConstraint.Axis) -> UIStackView { | |
let stackView = UIStackView() | |
stackView.axis = axis | |
stackView.alignment = .fill | |
stackView.translatesAutoresizingMaskIntoConstraints = false | |
stackView.distribution = .fillProportionally | |
return stackView | |
} | |
private func createAUIView(with backgroundColor: UIColor) -> UIView { | |
let newView = UIView() | |
newView.backgroundColor = backgroundColor | |
// newView.layer.borderColor = UIColor.white.cgColor | |
// newView.layer.borderWidth = 5 | |
newView.translatesAutoresizingMaskIntoConstraints = false | |
return newView | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment