ios view添加上边框_如何在UIView的顶部添加边框

My question is on the title.

I don't know how to add a border in a specific side, top or bottom, any side...

layer.border draws the border for the whole view...

解决方案

I consider subclassing UIView and overriding drawRect overkill here. Why not add an extension on UIView and add border subviews?

@discardableResult

func addBorders(edges: UIRectEdge,

color: UIColor,

inset: CGFloat = 0.0,

thickness: CGFloat = 1.0) -> [UIView] {

var borders = [UIView]()

@discardableResult

func addBorder(formats: String...) -> UIView {

let border = UIView(frame: .zero)

border.backgroundColor = color

border.translatesAutoresizingMaskIntoConstraints = false

addSubview(border)

addConstraints(formats.flatMap {

NSLayoutConstraint.constraints(withVisualFormat: $0,

options: [],

metrics: ["inset": inset, "thickness": thickness],

views: ["border": border]) })

borders.append(border)

return border

}

if edges.contains(.top) || edges.contains(.all) {

addBorder(formats: "V:|-0-[border(==thickness)]", "H:|-inset-[border]-inset-|")

}

if edges.contains(.bottom) || edges.contains(.all) {

addBorder(formats: "V:[border(==thickness)]-0-|", "H:|-inset-[border]-inset-|")

}

if edges.contains(.left) || edges.contains(.all) {

addBorder(formats: "V:|-inset-[border]-inset-|", "H:|-0-[border(==thickness)]")

}

if edges.contains(.right) || edges.contains(.all) {

addBorder(formats: "V:|-inset-[border]-inset-|", "H:[border(==thickness)]-0-|")

}

return borders

}

// Usage:

view.addBorder(edges: [.all]) // All with default arguments

view.addBorder(edges: [.top], color: .green) // Just Top, green, default thickness

view.addBorder(edges: [.left, .right, .bottom], color: .red, thickness: 3) // All except Top, red, thickness 3

With this code you're not tied to your subclass too, you can apply it to anything and everything that inherits from UIView - reusable in your project, and any others. Pass in other arguments to your methods to define other colours and widths. Many options.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS的画中画窗口中添加自定义视图,您需要按照以下步骤进行操作: 1.创建一个自定义视图,继承自UIView类。 2.在需要使用画中画功能的UIViewController中,添加以下代码以启用画中画: ```swift override func viewDidLoad() { super.viewDidLoad() // 允许画中画 self.isModalInPresentation = true } ``` 3.在需要添加自定义视图的UIViewController中,创建一个AVPlayerViewController实例,并将其添加到视图层次结构中: ```swift // 创建一个AVPlayerViewController实例 let playerViewController = AVPlayerViewController() // 添加到视图层次结构中 addChild(playerViewController) view.addSubview(playerViewController.view) playerViewController.didMove(toParent: self) ``` 4.在AVPlayerViewController实例中,使用AVPictureInPictureController的`playerViewController(_:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:)`方法,在画中画结束后恢复自定义视图: ```swift // 在画中画结束后恢复自定义视图 func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) { // 创建自定义视图 let customView = CustomView(frame: playerViewController.view.bounds) // 添加到视图层次结构中 playerViewController.view.addSubview(customView) playerViewController.view.bringSubviewToFront(customView) completionHandler(true) } ``` 请注意,您需要在info.plist文件中添加“Supported external accessory protocols”键,并将其设置为“com.apple.avfoundation.tunnel”的值,以便在iOS 14及更高版本中启用画中画功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值