6
Add a button.
The button detects touch events.
Example
Instructions
Edit ViewController.swift
- Open ViewController.swift
- Copy and paste code
Edit Main.storyboard
- Open Main.storyboard
- Click View
- Click Utilities
- Click Show Object Library
- Drag Button to View
- Click View
- Click Utilities
- Click Show Connections Inspector
- Under Sent Events, after Touch Up Inside, drag + to View Controller
- Click touchUpInside:
- Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
- Click interfaceBuilderButton
Run
- Click Product
- Click Run
Code
import UIKit class ViewController: UIViewController { @IBOutlet weak var interfaceBuilderButton: UIButton! @IBOutlet weak var interfaceBuilderLabel: UILabel! override func prefersStatusBarHidden() -> Bool { return true } override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.All.rawValue) } override func viewDidLoad() { super.viewDidLoad() self.interfaceBuilderLabel.backgroundColor = UIColor.blackColor() self.interfaceBuilderLabel.text = "Hello Xcode" self.interfaceBuilderLabel.textAlignment = NSTextAlignment.Center self.interfaceBuilderLabel.textColor = UIColor.whiteColor() self.view.removeConstraints(self.view.constraints()) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 20)) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 20)) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: -40)) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: -40)) self.interfaceBuilderButton.backgroundColor = UIColor.whiteColor() self.interfaceBuilderButton.setTitle("Touch", forState: UIControlState.Normal) self.interfaceBuilderButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -20)) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 20)) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderLabel, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: -40)) } @IBAction func touchUpInside(sender: UIButton!) { self.interfaceBuilderLabel.text = "Touched" } }