Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created October 24, 2019 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/605fb6aa0e346d82dc768ec2695c5720 to your computer and use it in GitHub Desktop.
Save anupamchugh/605fb6aa0e346d82dc768ec2695c5720 to your computer and use it in GitHub Desktop.
var imageView: UIImageView?
var button: UIButton?
var predictionLabel : UILabel?
func buildUI()
{
imageView = UIImageView(frame: .zero)
imageView?.image = UIImage(named: "placeholder")
imageView?.contentMode = .scaleAspectFit
imageView?.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(imageView!)
let aspectRatio = NSLayoutConstraint(item: imageView!, attribute: .width, relatedBy: .equal, toItem: imageView!, attribute: .height, multiplier: 1.0, constant: 0)
NSLayoutConstraint.activate([
imageView!.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 20),
imageView!.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
imageView!.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
aspectRatio
])
button = UIButton(type: .system)
button?.setTitle("Select Image", for: .normal)
button?.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(button!)
NSLayoutConstraint.activate([
button!.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: 0),
button!.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor, constant: 20),
button!.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -20),
button!.heightAnchor.constraint(equalToConstant: 50)
])
predictionLabel = UILabel(frame: .zero)
predictionLabel?.numberOfLines = 0
predictionLabel?.textAlignment = .center
predictionLabel?.text = "Prediction will be displayed here.."
predictionLabel?.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(predictionLabel!)
NSLayoutConstraint.activate([
predictionLabel!.bottomAnchor.constraint(equalTo: self.button!.topAnchor, constant: -20),
predictionLabel!.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor, constant: 20),
predictionLabel!.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -20),
predictionLabel!.topAnchor.constraint(equalTo: self.imageView!.bottomAnchor, constant: 20),
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment