6

Add a button.

The button detects touch events.

Example

6

Instructions

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

Edit ViewController.swift

  1. Open ViewController.swift
  2. Copy and paste code

Edit Main.storyboard

  1. Open Main.storyboard
  2. Click View
  3. Click Utilities
  4. Click Show Object Library
  5. Drag Button to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Sent Events, after Touch Up Inside, drag + to View Controller
  10. Click touchUpInside:
  11. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  12. Click interfaceBuilderButton

Run

  1. Click Product
  2. 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"
  }
}