aie
aie

Reputation: 65

Outline for Swift Buttons

I am new into swift programming and i'm trying to make a piano app. Does anyone know hout to put border at buttons in Swift ? I searched on internet but all tutorials are for an older version of Swift and it's not working anymore.

Upvotes: 3

Views: 3713

Answers (3)

nspavlo
nspavlo

Reputation: 385

UIButton inherits from UIControl and UIControl inherits from UIView. UIView contains CALayer (Core Animation layer) used for rendering. https://developer.apple.com/reference/quartzcore/calayer

import UIKit
import PlaygroundSupport

let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
button.backgroundColor = .red
button.layer.borderWidth = 2.0
button.layer.borderColor = UIColor.green.cgColor

PlaygroundPage.current.liveView = button

Upvotes: 4

Khalid Afridi
Khalid Afridi

Reputation: 923

Here is the code for swift 3

button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor.white.cgColor
// if you want corners to be rounded you can use the corner radus
button.layer.cornerRadius = 4.0
// if you're setting background image to the button and it happens to be not clipped then you can use 
button.clipsToBounds = true

Upvotes: -1

Ayman Karram
Ayman Karram

Reputation: 116

Try This

            button.layer.borderWidth = 1.0
            button.layer.borderColor = UIColor.whiteColor().CGColor
            button.layer.cornerRadius = 5.0
            button.clipsToBounds = true

You can easily change the Button attributes values like (borderWidth,borderColor,cornerRadius)

Upvotes: 0

Related Questions