ios - 为不适用于 ipad 的 View 添加边框

标签 ios swift

我根据这个答案创建了扩展:How to add a border just on the top side of a UIView

extension CALayer {

    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
        //https://stackoverflow.com/questions/17355280/how-to-add-a-border-just-on-the-top-side-of-a-uiview
        let border = CALayer()

        switch edge {
        case UIRectEdge.Top:
            border.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), thickness)
            break
        case UIRectEdge.Bottom:
            border.frame = CGRectMake(0, CGRectGetHeight(self.frame) - thickness, CGRectGetWidth(self.frame), thickness)
            break
        case UIRectEdge.Left:
            border.frame = CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame))
            break
        case UIRectEdge.Right:
            border.frame = CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame))
            break
        default:
            break
        }

        border.backgroundColor = color.CGColor;

        self.addSublayer(border)
    }
}

但在 Ipad 中我只看到 70% 的边框宽度:

enter image description here

我尝试将 frame 更改为 bounds 但它也不起作用。

最佳答案

View 框架在添加边框后调整大小。您在 viewDidLoad() 方法中添加边框对吗?尝试在 viewDidAppear() 方法中添加边框:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    yourView.layer.addBorder(UIRectEdge.Top, color: UIColor.redColor(), thickness: 0.5)
}

移动添加层
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.layer.addBorder(UIRectEdge.Top, color: UIColor.redColor(), thickness: 0.5)
}

关于ios - 为不适用于 ipad 的 View 添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35901835/

相关文章:

javascript - 使用javascript从网络应用程序强制链接在移动Safari中打开

ios - 如何上传数据到API?

Swift CallKit 识别

algorithm - 如何计算两个列表的增量(插入/删除/移动索引)?

ios - NSXMLParser parse() 只工作一次

swift - 在 Swift 中清除 UIWebView 的缓存

objective-c - 绘图层 :inContext - Unrecognized selector sent to instance

ios - UITableViewCell - 向上或向下滚动时内容重置

ios - PFQuery 与 UITableView 中的多个数组

ios - 将数据传递到本地 html iOS webview