skip to Main Content

Hello I am a very new and inexperienced developer and I am trying to border around a button. I’m using the Storyboard and when I used ViewController.swift, I can’t make the button a weak var. It is only allowing me to edit the actions. Please help, thank you!

enter image description here

When I usually do this it allows me to choose if I insert "Action, Outlet or Outlet Collection."

3

Answers


  1. Drag and release, then from the little menu that appears select outlet rather than action. Then, to set borders do something like:

    yourButton.layer.borderWidth = 10
    yourButton.layer.borderColor = UIColor.red.cgColor
    
    Login or Signup to reply.
  2. Your screenshot shows "Mates Scene" but your class name is ViewController. It also looks like "Mates Scene" has an embed segue to a ViewController, so it looks like you might be trying to add an IBOutlet to a child view controller from one of the parent view controller’s views (the "Home Button").

    If so, you can’t do that. You may have meant to add "Home Button" to the class of your "Mates" view controller instead.

    Login or Signup to reply.
  3. In recent versions of iOS Buttons don’t have any shape by default. They are displayed as clickable text.

    Make sure you are trying to connect your outlet and action from IB to the right target view controller, as suggested by Tyler.

    Once you’ve got the outlet and action links working you can add code to change the appearance.

    If you want to make your button a rounded rectangle, say, you can do that with code that manipulates the buttons’ layer settings (corner radius, borderWidth, borderColor, and backgroundColor are pretty common properties to edit.)

    Here is code that turns a button into a rounded rectangle withe a 1-pixel blue outline and a yellow background color:

    @IBOutlet weak var button: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let button = button {
            button.layer.cornerRadius = 5
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.blue.cgColor
            button.layer.backgroundColor = UIColor.yellow.cgColor
        }
    }
    

    Edit:

    That creates a button that looks like this:

    enter image description here

    (Note that if you use a border you may need to add some padding so that the border doesn’t "crowd" the button title as in my example.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search