【问题标题】:Rounded UIButton - iPad size圆形 UIButton - iPad 大小
【发布时间】:2017-10-22 11:23:24
【问题描述】:

我有自定义 UIButton 有圆边

import UIKit

class RoundedButton: UIButton {

    override init(frame: CGRect) {

        super.init(frame: frame)

        self.layer.cornerRadius = self.bounds.width * 0.5
        self.backgroundColor = UIColor(patternImage: UIImage(named: "map.png")!)
        self.setTitleColor(UIColor.white, for: UIControlState.normal)
    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        self.layer.cornerRadius = self.bounds.width * 0.5
        self.backgroundColor = UIColor(patternImage: UIImage(named: "map.png")!)
        self.setTitleColor(UIColor.white, for: UIControlState.normal)
    }

}

现在我在 UIViewcontroller 的 XIB 文件中使用这个“RoundedButton”(大小为 110、110),并且设置了约束以保持 UIViewcontrollers 视图的纵横比。

该按钮在 iPhone 模拟器中看起来是圆形的,但该按钮在 iPad 模拟器中不是圆形的。当我在 viewDidAppear 中设置 layer.cornerRadius 属性时,按钮在 iPad 模拟器中被四舍五入。

请看图片

我正在寻找一种替代解决方案,而不是在 viewDidappear 中再次重新定义图层角半径。

谢谢

【问题讨论】:

    标签: ios swift constraints


    【解决方案1】:

    像这样覆盖方法layoutSubviews:

    override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.cornerRadius = self.bounds.width * 0.5
    }
    

    您现在也可以从 init 中删除cornerRadius 线

    更多信息请查看 UIView 文档:https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews

    【讨论】:

      【解决方案2】:

      覆盖 layoutSubviews 函数:

      import UIKit
      
      class RoundedButton: UIButton {
      
          override init(frame: CGRect) {
              super.init(frame: frame)
      
              self.backgroundColor = UIColor(patternImage: UIImage(named: "map.png")!)
              self.setTitleColor(UIColor.white, for: UIControlState.normal)
          }
      
          required init?(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
      
              self.backgroundColor = UIColor(patternImage: UIImage(named: "map.png")!)
              self.setTitleColor(UIColor.white, for: UIControlState.normal)
          }
      
          override func layoutSubviews() {
              super.layoutSubviews()
              self.layer.cornerRadius = self.bounds.width * 0.5
          }
      }
      

      【讨论】:

        【解决方案3】:
        import UIKit
        
        class RoundedButton: UIButton {
        
            override init(frame: CGRect) {
                super.init(frame: frame)
                self.setTitleColor(UIColor.white, for: UIControlState.normal)
            }
        
            required init?(coder aDecoder: NSCoder) {
                super.init(coder: aDecoder)
                self.setTitleColor(UIColor.white, for: UIControlState.normal)
            }
        
            override func layoutSubviews() {
                super.layoutSubviews()
                self.layer.cornerRadius = self.bounds.width / 2
            }
        }
        

        我还为您创建了一个代码示例。 RoundedButton

        【讨论】:

          猜你喜欢
          • 2010-10-14
          • 2010-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-03
          • 1970-01-01
          • 2017-10-01
          • 1970-01-01
          相关资源
          最近更新 更多