Skip to content

Instantly share code, notes, and snippets.

@byteblocks
Created February 2, 2016 14:59
Show Gist options
  • Save byteblocks/9dcaba4a6e8ba50065d1 to your computer and use it in GitHub Desktop.
Save byteblocks/9dcaba4a6e8ba50065d1 to your computer and use it in GitHub Desktop.
public class ModalWindow : UIView{
var overlayWindow:UIView?
var infoStackWindow:UIStackView?
var titleLabel:UILabel?
var subTitleLabel:UILabel?
func initialize(){
backgroundColor = UIColor.blueColor()
alpha = 0.5
overlayWindow = UIView(frame: bounds)
overlayWindow!.backgroundColor = UIColor.greenColor()
overlayWindow!.translatesAutoresizingMaskIntoConstraints = false
createInformationStackView()
overlayWindow!.addSubview(infoStackWindow!)
addSubview(overlayWindow!)
addViewConstraints()
}
func createInformationStackView(){
infoStackWindow = UIStackView()
infoStackWindow!.axis = .Vertical
infoStackWindow!.alignment = .Center
infoStackWindow!.distribution = .Fill
infoStackWindow!.translatesAutoresizingMaskIntoConstraints = false
createTitle()
createSubtitle()
infoStackWindow!.addArrangedSubview(titleLabel!)
infoStackWindow!.addArrangedSubview(subTitleLabel!)
}
func createTitle(){
titleLabel = UILabel(frame: CGRectZero)
titleLabel!.text = "My Title"
titleLabel!.textAlignment = .Center
}
func createSubtitle(){
subTitleLabel = UILabel(frame: CGRectZero)
subTitleLabel!.text = "Test Subtitle"
subTitleLabel!.textAlignment = .Center
}
func addViewConstraints(){
autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
overlayWindow?.widthAnchor.constraintEqualToAnchor(widthAnchor).active = true
overlayWindow!.heightAnchor.constraintEqualToAnchor(heightAnchor).active = true
let xCenterConstraint = NSLayoutConstraint(item: infoStackWindow!, attribute: .CenterX, relatedBy: .Equal, toItem: infoStackWindow!.superview, attribute: .CenterX, multiplier: 1, constant: 0)
overlayWindow!.addConstraint(xCenterConstraint)
let yCenterConstraint = NSLayoutConstraint(item: infoStackWindow!, attribute: .CenterY, relatedBy: .Equal, toItem: infoStackWindow!.superview, attribute: .CenterY, multiplier: 1, constant: 0)
overlayWindow!.addConstraint(yCenterConstraint)
}
override public func layoutSubviews() {
super.layoutSubviews()
print("Overlay Bounds: \(overlayWindow!.bounds)")
print("Overlay Frame: \(overlayWindow!.frame)")
print("Stack Bounds: \(infoStackWindow!.bounds)")
print("Stack Frame:\(infoStackWindow!.frame)")
}
required public override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment