UIButton
UIButton detects touch events.
Example
Instructions
Create
- Open Xcode
- Click File
- Click New
- Click Project...
- Under iOS, click Application
- Click Single View Application
- Click Next
- After Product Name, type your product name
- After Organization Name, type your organization name or leave blank
- After Organization Identifier, type your organization identifier
- After Language, select Swift
- After Devices, select Universal
- Uncheck Use Core Data
- Click Next
- Select a folder
- After Source Control, uncheck Create Git repository on
- Click Create
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
Add Image
- Click File
- Click Add Files to "UIButton" ...
- Select image
- After Destination, check Copy items if needed
- After Added folders, select Create groups
- After Add to targets, check UIButton
- Click Add
Run
- Click Product
- Click Run
Code
import UIKit class ViewController: UIViewController { @IBOutlet weak var interfaceBuilderButton: UIButton! override func prefersStatusBarHidden() -> Bool { return true } override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.lightGrayColor() self.interfaceBuilderButton.setTitle("Interface Builder", forState: UIControlState.Normal) self.view.removeConstraints(self.view.constraints()) let systemButtonTypeButton = UIButton.buttonWithType(UIButtonType.System) as UIButton systemButtonTypeButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) systemButtonTypeButton.setTitle("System", forState: UIControlState.Normal) systemButtonTypeButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(systemButtonTypeButton) self.view.addConstraint(NSLayoutConstraint(item: systemButtonTypeButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let contactAddButtonTypeButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton contactAddButtonTypeButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) contactAddButtonTypeButton.setTitle("Contact Add", forState: UIControlState.Normal) contactAddButtonTypeButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(contactAddButtonTypeButton) self.view.addConstraint(NSLayoutConstraint(item: contactAddButtonTypeButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: systemButtonTypeButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let detailDisclosureButtonTypeButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton detailDisclosureButtonTypeButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) detailDisclosureButtonTypeButton.setTitle("Detail Disclosure", forState: UIControlState.Normal) detailDisclosureButtonTypeButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(detailDisclosureButtonTypeButton) self.view.addConstraint(NSLayoutConstraint(item: detailDisclosureButtonTypeButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contactAddButtonTypeButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let infoDarkButtonTypeButton = UIButton.buttonWithType(UIButtonType.InfoDark) as UIButton infoDarkButtonTypeButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) infoDarkButtonTypeButton.setTitle("Info Dark", forState: UIControlState.Normal) infoDarkButtonTypeButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(infoDarkButtonTypeButton) self.view.addConstraint(NSLayoutConstraint(item: infoDarkButtonTypeButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: detailDisclosureButtonTypeButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let infoLightButtonTypeButton = UIButton.buttonWithType(UIButtonType.InfoLight) as UIButton infoLightButtonTypeButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) infoLightButtonTypeButton.setTitle("Info Light", forState: UIControlState.Normal) infoLightButtonTypeButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(infoLightButtonTypeButton) self.view.addConstraint(NSLayoutConstraint(item: infoLightButtonTypeButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: infoDarkButtonTypeButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let customButtonTypeButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton customButtonTypeButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) customButtonTypeButton.setTitle("Custom", forState: UIControlState.Normal) customButtonTypeButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(customButtonTypeButton) self.view.addConstraint(NSLayoutConstraint(item: customButtonTypeButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: infoLightButtonTypeButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let titleButton = UIButton() titleButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) titleButton.setTitle("Title", forState: UIControlState.Normal) titleButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(titleButton) self.view.addConstraint(NSLayoutConstraint(item: titleButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: customButtonTypeButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let titleLabelButton = UIButton() titleLabelButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) titleLabelButton.setTitle("Title Label", forState: UIControlState.Normal) titleLabelButton.setTranslatesAutoresizingMaskIntoConstraints(false) titleLabelButton.titleLabel?.font = UIFont(name: "Times New Roman", size: 24) self.view.addSubview(titleLabelButton) self.view.addConstraint(NSLayoutConstraint(item: titleLabelButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let highlightedAttributedString = NSMutableAttributedString(string: "Attributed Title") highlightedAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSMakeRange(0, 10)) highlightedAttributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, 10)) highlightedAttributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial", size: 24)!, range: NSMakeRange(11, 5)) let normalAttributedString = NSMutableAttributedString(string: "Attributed Title") normalAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 10)) normalAttributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, 10)) normalAttributedString.addAttribute(NSFontAttributeName, value: UIFont(name: "Times New Roman", size: 24)!, range: NSMakeRange(11, 5)) let attributedTitleButton = UIButton() attributedTitleButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) attributedTitleButton.setAttributedTitle(highlightedAttributedString, forState: UIControlState.Highlighted) attributedTitleButton.setAttributedTitle(normalAttributedString, forState: UIControlState.Normal) attributedTitleButton.setTitle(highlightedAttributedString.string, forState: UIControlState.Highlighted) attributedTitleButton.setTitle(normalAttributedString.string, forState: UIControlState.Normal) attributedTitleButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(attributedTitleButton) self.view.addConstraint(NSLayoutConstraint(item: attributedTitleButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLabelButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let tintColorButton = UIButton.buttonWithType(UIButtonType.System) as UIButton tintColorButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) tintColorButton.setTitle("Tint Color", forState: UIControlState.Normal) tintColorButton.setTranslatesAutoresizingMaskIntoConstraints(false) tintColorButton.tintColor = UIColor.redColor() self.view.addSubview(tintColorButton) self.view.addConstraint(NSLayoutConstraint(item: tintColorButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: attributedTitleButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let titleColorButton = UIButton() titleColorButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) titleColorButton.setTitle("Title Color", forState: UIControlState.Normal) titleColorButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) titleColorButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(titleColorButton) self.view.addConstraint(NSLayoutConstraint(item: titleColorButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tintColorButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let titleShadowColorButton = UIButton() titleShadowColorButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) titleShadowColorButton.setTitle("Title Shadow Color", forState: UIControlState.Normal) titleShadowColorButton.setTitleShadowColor(UIColor.greenColor(), forState: UIControlState.Highlighted) titleShadowColorButton.setTitleShadowColor(UIColor.redColor(), forState: UIControlState.Normal) titleShadowColorButton.setTranslatesAutoresizingMaskIntoConstraints(false) titleShadowColorButton.titleLabel?.shadowOffset = CGSizeMake(2, 2) self.view.addSubview(titleShadowColorButton) self.view.addConstraint(NSLayoutConstraint(item: titleShadowColorButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleColorButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let reversesTitleShadowWhenHighlightedButton = UIButton() reversesTitleShadowWhenHighlightedButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) reversesTitleShadowWhenHighlightedButton.reversesTitleShadowWhenHighlighted = true reversesTitleShadowWhenHighlightedButton.setTitle("Reverses Title Shadow When Highlighted", forState: UIControlState.Normal) reversesTitleShadowWhenHighlightedButton.setTitleShadowColor(UIColor.greenColor(), forState: UIControlState.Highlighted) reversesTitleShadowWhenHighlightedButton.setTitleShadowColor(UIColor.redColor(), forState: UIControlState.Normal) reversesTitleShadowWhenHighlightedButton.setTranslatesAutoresizingMaskIntoConstraints(false) reversesTitleShadowWhenHighlightedButton.titleLabel?.shadowOffset = CGSizeMake(2, 2) self.view.addSubview(reversesTitleShadowWhenHighlightedButton) self.view.addConstraint(NSLayoutConstraint(item: reversesTitleShadowWhenHighlightedButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleShadowColorButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let imageButton = UIButton() imageButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) imageButton.setImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) imageButton.setTitle("Image", forState: UIControlState.Normal) imageButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(imageButton) self.view.addConstraint(NSLayoutConstraint(item: imageButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: reversesTitleShadowWhenHighlightedButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let adjustsImageWhenDisabledButton = UIButton() adjustsImageWhenDisabledButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) adjustsImageWhenDisabledButton.adjustsImageWhenDisabled = true adjustsImageWhenDisabledButton.enabled = false adjustsImageWhenDisabledButton.setImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) adjustsImageWhenDisabledButton.setTitle("Adjusts Image When Disabled", forState: UIControlState.Normal) adjustsImageWhenDisabledButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(adjustsImageWhenDisabledButton) self.view.addConstraint(NSLayoutConstraint(item: adjustsImageWhenDisabledButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: imageButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let adjustsImageWhenHighlightedButton = UIButton() adjustsImageWhenHighlightedButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) adjustsImageWhenHighlightedButton.adjustsImageWhenHighlighted = false adjustsImageWhenHighlightedButton.setImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) adjustsImageWhenHighlightedButton.setTitle("Adjusts Image When Highlighted", forState: UIControlState.Normal) adjustsImageWhenHighlightedButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(adjustsImageWhenHighlightedButton) self.view.addConstraint(NSLayoutConstraint(item: adjustsImageWhenHighlightedButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: adjustsImageWhenDisabledButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let showsTouchWhenHighlightedButton = UIButton() showsTouchWhenHighlightedButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) showsTouchWhenHighlightedButton.setImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) showsTouchWhenHighlightedButton.setTitle("Shows Touch When Highlighted", forState: UIControlState.Normal) showsTouchWhenHighlightedButton.setTranslatesAutoresizingMaskIntoConstraints(false) showsTouchWhenHighlightedButton.showsTouchWhenHighlighted = true self.view.addSubview(showsTouchWhenHighlightedButton) self.view.addConstraint(NSLayoutConstraint(item: showsTouchWhenHighlightedButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: adjustsImageWhenHighlightedButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let contentEdgeInsetsButton = UIButton() contentEdgeInsetsButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) contentEdgeInsetsButton.contentEdgeInsets = UIEdgeInsetsMake(0, 40, 0, -40) contentEdgeInsetsButton.setImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) contentEdgeInsetsButton.setTitle("Content Edge Insets", forState: UIControlState.Normal) contentEdgeInsetsButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(contentEdgeInsetsButton) self.view.addConstraint(NSLayoutConstraint(item: contentEdgeInsetsButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: showsTouchWhenHighlightedButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let imageEdgeInsetsButton = UIButton() imageEdgeInsetsButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) imageEdgeInsetsButton.imageEdgeInsets = UIEdgeInsetsMake(0, 40, 0, -40) imageEdgeInsetsButton.setImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) imageEdgeInsetsButton.setTitle("Image Edge Insets", forState: UIControlState.Normal) imageEdgeInsetsButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) imageEdgeInsetsButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(imageEdgeInsetsButton) self.view.addConstraint(NSLayoutConstraint(item: imageEdgeInsetsButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentEdgeInsetsButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let titleEdgeInsetsButton = UIButton() titleEdgeInsetsButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) titleEdgeInsetsButton.setImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) titleEdgeInsetsButton.setTitle("Title Edge Insets", forState: UIControlState.Normal) titleEdgeInsetsButton.setTranslatesAutoresizingMaskIntoConstraints(false) titleEdgeInsetsButton.titleEdgeInsets = UIEdgeInsetsMake(0, 40, 0, -40) self.view.addSubview(titleEdgeInsetsButton) self.view.addConstraint(NSLayoutConstraint(item: titleEdgeInsetsButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: imageEdgeInsetsButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let backgroundImageButton = UIButton() backgroundImageButton.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) backgroundImageButton.setBackgroundImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) backgroundImageButton.setTitle("Background Image", forState: UIControlState.Normal) backgroundImageButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) backgroundImageButton.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(backgroundImageButton) self.view.addConstraint(NSLayoutConstraint(item: backgroundImageButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleEdgeInsetsButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) } @IBAction func touchUpInside(sender: UIButton!) { //let alertView = UIAlertView(title: sender.currentTitle, message: nil, delegate: nil, cancelButtonTitle: "Cancel") //alertView.show() let alertController = UIAlertController(title: sender.currentTitle, message: nil, preferredStyle: UIAlertControllerStyle.Alert) alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)) self.presentViewController(alertController, animated: true, completion: nil) } }