5

Add a label.

The label displays static text.

Example

5

Instructions

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

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 Label to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  10. Click interfaceBuilderLabel

Run

  1. Click Product
  2. Click Run

Code

import UIKit

class ViewController: UIViewController
{
  @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))
  }
}