【问题标题】:How to remove right and left borders in UIButton?如何删除 UIButton 中的左右边框?
【发布时间】:2016-10-13 20:14:46
【问题描述】:

我现在使用的是 Swift 2.3。我已经使用以下代码为按钮添加了边框:

self.history.layer.borderWidth = 1
self.history.layer.borderColor = UIColor.mainColor().CGColor

结果我得到:

现在,我需要删除左右边框,只剩下顶部和底部边框。我该如何管理它?

补充:感谢@Teja,我看到了另一个问题CALayer: add a border only at one side 但是,问题是我需要底部和顶部。但是添加-1、-1 CALayer后只添加了最下面的。

【问题讨论】:

标签: ios swift


【解决方案1】:

[SWIFT 4.2]

我试图显示底部边框有这个结果:

我改进了上面帖子的扩展以添加边距..

extension UIButton {

     func drawBorder(edges: [UIRectEdge], borderWidth: CGFloat, color: UIColor, margin: CGFloat) {
        for item in edges {
            let borderLayer: CALayer = CALayer()
            borderLayer.borderColor = color.cgColor
            borderLayer.borderWidth = borderWidth
            switch item {
            case .top:
                borderLayer.frame = CGRect(x: margin, y: 0, width: frame.width - (margin*2), height: borderWidth)
            case .left:
                borderLayer.frame =  CGRect(x: 0, y: margin, width: borderWidth, height: frame.height - (margin*2))
            case .bottom:
                borderLayer.frame = CGRect(x: margin, y: frame.height - borderWidth, width: frame.width - (margin*2), height: borderWidth)
            case .right:
                borderLayer.frame = CGRect(x: frame.width - borderWidth, y: margin, width: borderWidth, height: frame.height - (margin*2))
            case .all:
                drawBorder(edges: [.top, .left, .bottom, .right], borderWidth: borderWidth, color: color, margin: margin)
            default:
                break
            }
            self.layer.addSublayer(borderLayer)
        }
    }

}

使用:

mybutton.drawBorder(edges: [.bottom], borderWidth: 1, color: UIColor.darkGray, margin: 20)

效果很好:)

【讨论】:

    【解决方案2】:

    您可以尝试以下方法。

    let topBorder = CALayer()
    topBorder.borderColor = UIColor.black.cgColor;
    topBorder.borderWidth = 1;
    topBorder.frame = CGRect(x: 0, y: 0, width: label.frame.width, height: 1)
    label.layer.addSublayer(topBorder)
    
    let bottomBorder = CALayer()
    bottomBorder.borderColor = UIColor.black.cgColor;
    bottomBorder.borderWidth = 1;
    bottomBorder.frame = CGRect(x: 0, y: label.frame.height, width: label.frame.width, height: 1)
    label.layer.addSublayer(bottomBorder)
    

    【讨论】:

    • 谢谢!我就是这么做的
    • 这段代码帮助很大。感谢您的解决方案。
    【解决方案3】:

    您可以添加多个图层而不是边框​​,但您不能将角半径影响到这些边框,顺便说一下,这里只是为您的项目添加边框

    import UIKit
    
    extension UIView {
    
        func drawBorder(edges: [UIRectEdge], borderWidth: CGFloat, color: UIColor) {
            for item in edges {
                let borderLayer: CALayer = CALayer()
                borderLayer.borderColor = color.cgColor
                borderLayer.borderWidth = borderWidth
                switch item {
                case .top:
                    borderLayer.frame = CGRect(x: 0, y: 0, width: frame.width, height: borderWidth)
                case .left:
                    borderLayer.frame =  CGRect(x: 0, y: 0, width: borderWidth, height: frame.height)
                case .bottom:
                    borderLayer.frame = CGRect(x: 0, y: frame.height - borderWidth, width: frame.width, height: borderWidth)
                case .right:
                    borderLayer.frame = CGRect(x: frame.width - borderWidth, y: 0, width: borderWidth, height: frame.height)
                case .all:
                    drawBorder(edges: [.top, .left, .bottom, .right], borderWidth: borderWidth, color: color)
                default:
                    break
                }
                self.layer.addSublayer(borderLayer)
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      我通过添加这两个 CALayers 解决了我的问题:

      let bottomBorder: CALayer = CALayer()
      let topBorder: CALayer = CALayer()
      bottomBorder.borderColor = UIColor.mainColor().CGColor
      topBorder.borderColor = UIColor.mainColor().CGColor
      bottomBorder.borderWidth = 1
      topBorder.borderWidth = 1
      bottomBorder.frame =  CGRectMake(0, CGRectGetHeight(history.frame), CGRectGetWidth(history.frame), 1)
      topBorder.frame = CGRectMake(0, 0, CGRectGetWidth(history.frame), 1)
      history.layer.addSublayer(bottomBorder)
      history.layer.addSublayer(topBorder)
      

      非常感谢@Teja 和@Rajan!

      【讨论】:

        【解决方案5】:

        另一种方法是添加 UIView 作为边框,高度或宽度作为边框宽度。您可以根据您的使用情况将它们全部隐藏/显示,或者只为其添加顶部和底部视图。

        如果您不是以编程方式创建视图并且需要使用约束,这将非常有用。

        【讨论】:

          猜你喜欢
          • 2016-09-26
          • 2012-02-22
          • 1970-01-01
          • 1970-01-01
          • 2017-12-06
          • 1970-01-01
          • 2018-12-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多