Contact Us

Use the form on the right to contact us.

You can edit the text in this area, and change where the contact form on the right submits to, by entering edit mode using the modes on the bottom right. 


Oak Ridge, TN, 37830
United States

Swift-Snips

Code a Navigation Button

Wade Cantley

For some situations a navigation button just isn't going to work in the storyboard.  For this, I have found that I need to add a button programatically.  Here are the steps.

1) Create a Receiving function that the button will trigger. This is the equivalent of an IBAction except without the "IBAction" part.

2) Put the button code in the ViewDidLoad() method.


Step 1)  For this example, this function is supposed to kick of the refresh of onboarding.  I took the body out to indicate that it has nothing to do with the button part.  


func onBoardingRefresh(sender: UIButton) {
        //Do something here.
    }

Step 2)  Now we create the button, and put it into a barButtonItem.  Note that I already have an image named "InfoButton" in the system.  This name is whatever name is on the image you want to use.


override func viewDidLoad() {
        super.viewDidLoad()

// Setup the button object and give it the properties it needs
var onboardingButton: UIButton = UIButton()
            onboardingButton.setImage(UIImage(named: "InfoButton"), forState: .Normal)
            onboardingButton.frame = CGRectMake(0, 0, 22, 22)
            onboardingButton.addTarget(self, action: "onBoardingRefresh:", forControlEvents: UIControlEvents.TouchUpInside)
            
	//Setup the ButtonBarItem class and add the button to it and indicate where.
            var rightItem:UIBarButtonItem = UIBarButtonItem()
            rightItem.customView = onboardingButton
            self.navigationItem.rightBarButtonItem = rightItem

}