如何删除UIButton中的左右边框?

时间:2016-10-13 20:14:47

标签: ios swift

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

# Provide a PEP 3115 compliant mechanism for class creation
def new_class(name, bases=(), kwds=None, exec_body=None):
    """Create a class object dynamically using the appropriate metaclass."""
    meta, ns, kwds = prepare_class(name, bases, kwds)
    if exec_body is not None:
        exec_body(ns)
    return meta(name, bases, ns, **kwds)

def prepare_class(name, bases=(), kwds=None):
    """Call the __prepare__ method of the appropriate metaclass.

    Returns (metaclass, namespace, kwds) as a 3-tuple

    *metaclass* is the appropriate metaclass
    *namespace* is the prepared class namespace
    *kwds* is an updated copy of the passed in kwds argument with any
    'metaclass' entry removed. If no kwds argument is passed in, this will
    be an empty dict.
    """
    if kwds is None:
        kwds = {}
    else:
        kwds = dict(kwds) # Don't alter the provided mapping
    if 'metaclass' in kwds:
        meta = kwds.pop('metaclass')
    else:
        if bases:
            meta = type(bases[0])
        else:
            meta = type
    if isinstance(meta, type):
        # when meta is a type, we first determine the most-derived metaclass
        # instead of invoking the initial candidate directly
        meta = _calculate_meta(meta, bases)
    if hasattr(meta, '__prepare__'):
        ns = meta.__prepare__(name, bases, **kwds)
    else:
        ns = {}
    return meta, ns, kwds


def _calculate_meta(meta, bases):
    """Calculate the most derived metaclass."""
    winner = meta
    for base in bases:
        base_meta = type(base)
        if issubclass(winner, base_meta):
            continue
        if issubclass(base_meta, winner):
            winner = base_meta
            continue
        # else:
        raise TypeError("metaclass conflict: "
                        "the metaclass of a derived class "
                        "must be a (non-strict) subclass "
                        "of the metaclasses of all its bases")
    return winner

结果我得到了: History button borders

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

补充:thnx到@Teja,我已经看到了另一个问题CALayer: add a border only at one side 但是,问题是我需要底部和顶部。但是,在添加-1之后,-1 CALayer只添加了底部。

5 个答案:

答案 0 :(得分:4)

您可以尝试下面的内容。

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)

答案 1 :(得分:1)

我通过添加这两个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!

答案 2 :(得分:1)

您可以添加几层而不是边界,但是您不能像边界那样影响拐角半径,因为这只是在项目中添加边界

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)
        }
    }
}

答案 3 :(得分:0)

另一种方法是将UIView添加为边框,高度或宽度添加为borderWidth。您可以根据自己的用途将它们全部隐藏/显示,也可以仅为其添加顶视图和底视图。

如果您不是以编程方式创建视图并且需要使用约束,那么这真的很有用。

答案 4 :(得分:0)

[SWIFT 4.2]

我试图显示底部边框以产生此结果:

enter image description here

我改进了上面文章的扩展名,以增加边距。

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)

效果很好:)