Ben*_*Ben 5 subclass uibutton swift
使用swift,我想创建一个带圆角的按钮.为了使这个可重用,我的偏好是UIButton的子类,并提出以下内容:
import Foundation
import UIKit
class LoginButton: UIButton {
let corner_radius : CGFloat = 4.0
override func drawRect(rect: CGRect) {
super.drawRect(rect)
self.layer.cornerRadius = corner_radius
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎并不像我希望的那样有效,即使它编译得很好.也许我错过了一些东西 - 我对此很新!
有任何想法吗?
Swi*_*y89 10
您需要使用clipsToBounds以确保不在角半径上绘制包含视图:
self.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)