Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Nicklas Lind
Nicklas Lind
3,619 Points

How do I make a rounded button with a border in Swift?

Hello,

I've been following along the (excellent!) "Build a simple iOS app with Swift" course.

I decided to venture a bit further than the bounds of the course and wanted to add a rounded rectangular button with a border (like the ones in the bottom of http://cooking.nytimes.com) and I found out how to make round the button out from a YouTube video, but not how to add the border.

Can somebody please help me? Any help would be much appreciated!

Thank you in advance.

Nicklas

My progress thus far:

alt text

3 Answers

Liam Herbert
Liam Herbert
15,140 Points

To round a button you add this code to your ViewController.swift under the viewDidLoad() method:

/*Object name here*/.layer.cornerRadius = 10

Hope this helps

i am trying to get rounded edges but this is not working in my code, do you have any ideas why? It is under the view did load but I included my entire code in case you needed it.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var currentAqiLabel: UILabel!

@IBOutlet weak var currentAqiColor: UILabel!

@IBOutlet weak var currentAqiDescription: UILabel!

@IBOutlet weak var currentCountryAqi: UILabel!

@IBOutlet weak var currentCountryColor: UILabel!


@IBOutlet weak var currentCountryDescription: UILabel!

@IBOutlet weak var refreshButton: UIButton!

@IBOutlet weak var activityIndicator: UIActivityIndicatorView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  retrieveAqiForecast()

  self.currentCountryColor.layer.cornerRadius = 7
  currentAqiColor.layer.cornerRadius = 7

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}

func retrieveAqiForecast() {

let forecastService = ForecastService()
    forecastService.getForecast() {
        (let result: NSDictionary) in

        dispatch_async (dispatch_get_main_queue()) {

        if let breezeAqi = result["breezometer_aqi"] {
            self.currentAqiLabel?.text = "\(breezeAqi)"
        }
        else {
            self.currentAqiLabel?.text = "Unknown"
        }

        if let breezeColor = result["breezometer_color"] {
            self.currentAqiColor?.text = "\(breezeColor)"
            self.currentAqiColor?.textColor = UIColor(hexString: "\(breezeColor)")
            self.currentAqiColor?.backgroundColor = UIColor(hexString: "\(breezeColor)")
        }
        else {
            self.currentAqiColor?.text = "Unknown"
        }

        if let breezeDescription = result["breezometer_description"] {
            self.currentAqiDescription?.text = "\(breezeDescription)"
        }
        else {
            self.currentAqiDescription?.text = "Unknown"
        }

        if let countryAqi = result["country_aqi"] {
            self.currentCountryAqi?.text = "\(countryAqi)"
        }
        else {
            self.currentCountryAqi?.text = "Unknown"
        }

        if let countryColor = result["country_color"] {
            self.currentCountryColor?.text = "\(countryColor)"
            self.currentCountryColor?.textColor =  UIColor(hexString: "\(countryColor)")
            self.currentCountryColor?.backgroundColor =  UIColor(hexString: "\(countryColor)")
        }
        else {
            self.currentCountryColor?.text = "Unknown"
        }

        if let countryDescription = result["country_description"] {
            self.currentCountryDescription?.text = "\(countryDescription)"
        }
        else {
            self.currentCountryDescription?.text = "Unknown"
                }
        self.toggleRefreshAnimation(false)

            }
        }
    }


@IBAction func refreshAqi() {
    toggleRefreshAnimation(true)
    retrieveAqiForecast()
}

func toggleRefreshAnimation(on: Bool) {
    refreshButton?.hidden = on
    if on {
        activityIndicator?.startAnimating()
    } else
    {
        activityIndicator?.stopAnimating()
        }
    }

}