加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

带圆角的UIButton子类(swift)

发布时间:2020-12-14 04:50:18 所属栏目:百科 来源:网络整理
导读:使用 swift,我想创建一个带圆角的按钮.为了使这个可重用,我的偏好是UIButton的子类,并提出以下内容: import Foundationimport UIKitclass LoginButton: UIButton { let corner_radius : CGFloat = 4.0 override func drawRect(rect: CGRect) { super.drawRe
使用 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
  }

}

不幸的是,这似乎并不像我希望的那样有效,即使它编译得很好.也许我错过了一些东西 – 我对此很新!

有任何想法吗?

解决方法

您需要使用clipsToBounds来确保不在角半径上绘制包含视图:

self.clipsToBounds = true

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读