在UIView外部添加边框(而不是在内部)

时间:2013-03-03 09:06:12

标签: ios uiview border

如果使用视图中的代码添加视图的边框,例如

self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = 2.0f;

边框在视图中添加如下: enter image description here

右视图是原始视图,如您所见,边界视图的黑色区域小于原始视图。但我想得到的是原始视图之外的边框,如下所示:enter image description here。黑色区域等于原始区域,我该如何实现呢?

11 个答案:

答案 0 :(得分:95)

不幸的是,您可以设置一个小的属性来将边框与外部对齐。它绘制与内部对齐,因为UIViews默认绘制操作在其边界内绘制。

最简单的解决方案是在应用边框时按边框宽度的大小扩展UIView:

CGFloat borderWidth = 2.0f;

self.frame = CGRectInset(self.frame, -borderWidth, -borderWidth);
self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = borderWidth;

答案 1 :(得分:19)

好的,已经有一个已接受的答案,但我认为有一个更好的方法,你只需要一个比你的视图大一点的新图层,不要将它掩盖到视图的边界&#39 ; s层(实际上是默认行为)。以下是示例代码:

CALayer * externalBorder = [CALayer layer];
externalBorder.frame = CGRectMake(-1, -1, myView.frame.size.width+2, myView.frame.size.height+2);
externalBorder.borderColor = [UIColor blackColor].CGColor;
externalBorder.borderWidth = 1.0;

[myView.layer addSublayer:externalBorder];
myView.layer.masksToBounds = NO;

当然,如果您希望边框大于1个整数,如果您想要更多,则相应地调整borderWidth和图层的框架。 这比使用稍大一点的第二个视图更好,因为CALayerUIView更轻,并且你没有修改myView的框架,这对于如果myViewUIImageView

,则为实例

NB:对我来说,结果在模拟器上并不完美(图层并不完全在正确的位置,所以有时候一侧的图层厚一些)但正是真正要求的设备

编辑

实际上我在 N.B 中谈到的问题仅仅是因为我减少了模拟器的屏幕,在正常尺寸上完全没有问题

希望有所帮助

答案 2 :(得分:16)

通过以上公认的最佳答案,我体验了这种不好结果和难看的边缘:

border without bezier path

所以我将与你分享我的UIView Swift 扩展程序,它使用UIBezierPath作为边框轮廓 - 没有难看的边缘(受@Fattie启发):

border with bezier path

//  UIView+BezierPathBorder.swift

import UIKit

extension UIView {

    fileprivate var bezierPathIdentifier:String { return "bezierPathBorderLayer" }

    fileprivate var bezierPathBorder:CAShapeLayer? {
        return (self.layer.sublayers?.filter({ (layer) -> Bool in
            return layer.name == self.bezierPathIdentifier && (layer as? CAShapeLayer) != nil
        }) as? [CAShapeLayer])?.first
    }

    func bezierPathBorder(_ color:UIColor = .white, width:CGFloat = 1) {

        var border = self.bezierPathBorder
        let path = UIBezierPath(roundedRect: self.bounds, cornerRadius:self.layer.cornerRadius)
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask

        if (border == nil) {
            border = CAShapeLayer()
            border!.name = self.bezierPathIdentifier
            self.layer.addSublayer(border!)
        }

        border!.frame = self.bounds
        let pathUsingCorrectInsetIfAny =
            UIBezierPath(roundedRect: border!.bounds, cornerRadius:self.layer.cornerRadius)

        border!.path = pathUsingCorrectInsetIfAny.cgPath
        border!.fillColor = UIColor.clear.cgColor
        border!.strokeColor = color.cgColor
        border!.lineWidth = width * 2
    }

    func removeBezierPathBorder() {
        self.layer.mask = nil
        self.bezierPathBorder?.removeFromSuperlayer()
    }

}

示例:

let view = UIView(frame: CGRect(x: 20, y: 20, width: 100, height: 100))
view.layer.cornerRadius = view.frame.width / 2
view.backgroundColor = .red

//add white 2 pixel border outline
view.bezierPathBorder(.white, width: 2)

//remove border outline (optional)
view.removeBezierPathBorder()

答案 3 :(得分:13)

没有直接的方法可以做到这一点 你可以考虑一些解决方法。

  1. 更改并增加框架并添加bordercolor(
  2. 在当前视图后面添加一个较大尺寸的视图,使其显示为边框。可以作为自定义视图类
  3. 如果您不需要明确的边框(clearcut border),那么您可以依靠阴影来实现此目的

    [view1 setBackgroundColor:[UIColor blackColor]];
    UIColor *color = [UIColor yellowColor];
    view1.layer.shadowColor = [color CGColor];
    view1.layer.shadowRadius = 10.0f;
    view1.layer.shadowOpacity = 1;
    view1.layer.shadowOffset = CGSizeZero;
    view1.layer.masksToBounds = NO;
    

答案 4 :(得分:10)

对于Swift实现,您可以将其添加为UIView扩展。

extension UIView {

    struct Constants {
        static let ExternalBorderName = "externalBorder"
    }

    func addExternalBorder(borderWidth: CGFloat = 2.0, borderColor: UIColor = UIColor.whiteColor()) -> CALayer {
        let externalBorder = CALayer()
        externalBorder.frame = CGRectMake(-borderWidth, -borderWidth, frame.size.width + 2 * borderWidth, frame.size.height + 2 * borderWidth)
        externalBorder.borderColor = borderColor.CGColor
        externalBorder.borderWidth = borderWidth
        externalBorder.name = Constants.ExternalBorderName

        layer.insertSublayer(externalBorder, atIndex: 0)
        layer.masksToBounds = false

        return externalBorder
    }

    func removeExternalBorders() {
        layer.sublayers?.filter() { $0.name == Constants.ExternalBorderName }.forEach() {
            $0.removeFromSuperlayer()
        }
    }

    func removeExternalBorder(externalBorder: CALayer) {
        guard externalBorder.name == Constants.ExternalBorderName else { return }
        externalBorder.removeFromSuperlayer()
    }

}

答案 5 :(得分:2)

在添加边框之前,使用边框宽度增加视图框架的宽度和高度:

float borderWidth = 2.0f
CGRect frame = self.frame;
frame.width += borderWidth;
frame.height += borderWidth;
 self.layer.borderColor = [UIColor yellowColor].CGColor;
 self.layer.borderWidth = 2.0f;

答案 6 :(得分:1)

实际上有一个非常简单的解决方案。只需将它们设置如下:

view.layer.borderWidth = 5

view.layer.borderColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).cgColor

view.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.25).cgColor

答案 7 :(得分:0)

我喜欢 @picciano 的解决方案 如果要爆炸圆形而不是正方形,请用以下方法替换 addExternalBorder 函数:

func addExternalBorder(borderWidth: CGFloat = 2.0, borderColor: UIColor = UIColor.white) {
        let externalBorder = CALayer()
        externalBorder.frame = CGRect(x: -borderWidth, y: -borderWidth, width: frame.size.width + 2 * borderWidth, height: frame.size.height + 2 * borderWidth)
        externalBorder.borderColor = borderColor.cgColor
        externalBorder.borderWidth = borderWidth
        externalBorder.cornerRadius = (frame.size.width + 2 * borderWidth) / 2
        externalBorder.name = Constants.ExternalBorderName
        layer.insertSublayer(externalBorder, at: 0)
        layer.masksToBounds = false

    }

答案 8 :(得分:0)

我喜欢@picciano和@Maksim Kniazev的解决方案。我们还可以使用以下内容创建环形边框:

func addExternalAnnularBorder(borderWidth: CGFloat = 2.0, borderColor: UIColor = UIColor.white) {
    let externalBorder = CALayer()
    externalBorder.frame = CGRect(x: -borderWidth*2, y: -borderWidth*2, width: frame.size.width + 4 * borderWidth, height: frame.size.height + 4 * borderWidth)
    externalBorder.borderColor = borderColor.cgColor
    externalBorder.borderWidth = borderWidth
    externalBorder.cornerRadius = (frame.size.width + 4 * borderWidth) / 2
    externalBorder.name = Constants.ExternalBorderName
    layer.insertSublayer(externalBorder, at: 0)
    layer.masksToBounds = false
}

答案 9 :(得分:0)

我如何在Storyboard中的UI视图(主要-SubscriptionAd)周围放置边框,是将其放置在另一个UI视图(背景-BackgroundAd)中。 Background UIView的背景颜色与我想要的边框颜色相匹配,并且Main UIView的每侧约束值均为2。

我将背景视图链接到ViewController,然后通过更改背景颜色来打开和关闭边框。

Image Of Nested UIView with Top View 2px constraints all around, making it smaller than larger

答案 10 :(得分:0)

雨燕5

extension UIView {
    fileprivate struct Constants {
        static let externalBorderName = "externalBorder"
    }

    func addExternalBorder(borderWidth: CGFloat = 2.0, borderColor: UIColor = UIColor.white) -> CALayer {
        let externalBorder = CALayer()
        externalBorder.frame = CGRect(x: -borderWidth, y: -borderWidth, width: frame.size.width + 2 * borderWidth, height: frame.size.height + 2 * borderWidth)
        externalBorder.borderColor = borderColor.cgColor
        externalBorder.borderWidth = borderWidth
        externalBorder.name = Constants.ExternalBorderName

        layer.insertSublayer(externalBorder, at: 0)
        layer.masksToBounds = false

        return externalBorder
    }

    func removeExternalBorders() {
        layer.sublayers?.filter() { $0.name == Constants.externalBorderName }.forEach() {
            $0.removeFromSuperlayer()
        }
    }

    func removeExternalBorder(externalBorder: CALayer) {
        guard externalBorder.name == Constants.externalBorderName else { return }
        externalBorder.removeFromSuperlayer()
    }
}