Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oliverfoggin/9ce9008ddf22974f963a3cfbd565b629 to your computer and use it in GitHub Desktop.
Save oliverfoggin/9ce9008ddf22974f963a3cfbd565b629 to your computer and use it in GitHub Desktop.
Buy Button
import UIKit
class BuyButton: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//MARK: - View
backgroundColor = UIColor(red: 180/255, green: 35/255, blue: 115/255, alpha: 1)
layer.cornerRadius = 3
//MARK: - Price View
let priceView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: bounds.size.height))
priceView.backgroundColor = UIColor.black
addSubview(priceView)
//MARK: - Purchase View
let purchaseView = UIView(frame: CGRect(x: 80, y: 0, width: bounds.size.width - 80, height: bounds.size.height))
purchaseView.backgroundColor = UIColor.red
addSubview(purchaseView)
//MARK: - Price Label
let priceLabel = UILabel(frame: CGRect.zero)
priceLabel.text = "$ 50.00"
priceLabel.textAlignment = .center
priceLabel.backgroundColor = .red
priceLabel.translatesAutoresizingMaskIntoConstraints = false
priceView.addSubview(priceLabel)
//MARK: - Purchase Label
let purchaseLabel = UILabel(frame: CGRect.zero)
purchaseLabel.text = "Purchase"
purchaseLabel.textAlignment = .center
purchaseLabel.backgroundColor = .green
purchaseLabel.translatesAutoresizingMaskIntoConstraints = false
purchaseView.addSubview(purchaseLabel)
//MARK: - View Constraints
priceView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
priceView.topAnchor.constraint(equalTo: topAnchor).isActive = true
priceView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
purchaseView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
purchaseView.topAnchor.constraint(equalTo: topAnchor).isActive = true
purchaseView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
priceView.centerXAnchor.constraint(equalTo: purchaseView.centerXAnchor).isActive = true
//MARK: - Label Constraints
priceLabel.centerXAnchor.constraint(equalTo: priceView.centerXAnchor).isActive = true
priceLabel.centerYAnchor.constraint(equalTo: priceView.centerYAnchor).isActive = true
purchaseLabel.centerXAnchor.constraint(equalTo: purchaseView.centerXAnchor).isActive = true
purchaseLabel.centerYAnchor.constraint(equalTo: purchaseView.centerYAnchor).isActive = true
//MARK: - Constraint priorities
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment