我可以用这种方式为CALayer添加边框:
[webView.layer setBorderColor: [[UIColor colorWithRed:0.6 green:0.7 blue:0.2 alpha:1] CGColor]]; [webView.layer setBorderWidth: 2.75];
但是,是否可以仅在一侧添加边框?我只需要底部的边框.或者我可以用其他属性来达到这个目的,例如框架,边界,面具,......?
谢谢你的帮助!
b控制-V
UIWebView *webView = [[UIWebView alloc] init]; CALayer *webViewLayer = webView.layer; // now you can do a lot of stuff like borders: [webViewLayer setBorderColor: [[UIColor greenColor] CGColor]]; [webViewLayer setBorderWidth: 2.75];
请查看CALayer文档:https: //developer.apple.com/documentation/quartzcore/calayer
看看这里:http: //iosdevelopertips.com/cocoa/add-rounded-corners-and-border-to-uiwebview.html
我用这个做了一个右边框:
leftScrollView.clipsToBounds = YES; CALayer *rightBorder = [CALayer layer]; rightBorder.borderColor = [UIColor darkGrayColor].CGColor; rightBorder.borderWidth = 1; rightBorder.frame = CGRectMake(-1, -1, CGRectGetWidth(leftScrollView.frame), CGRectGetHeight(leftScrollView.frame)+2); [leftScrollView.layer addSublayer:rightBorder];
最简单的方法是添加一个将绘制选择性边框的subLayer,但是在选择此解决方案时需要考虑一些事项,最大的方法是确保边框subLayer始终位于顶部,并且当您更改边框时边框会发生变化你的图层的框架.
我实现了一个解决这些问题的开源解决方案,让你声明这样的选择性边框:
myView.borderDirection = AUIFlexibleBordersDirectionRight | AUIFlexibleBordersDirectionTop;
您可以获取代码,并在此处阅读更多内容
border属性始终为视图的4个边添加边框.您可以使用自己的绘制方法在视图底部绘制边框.
但是,为什么不在UIWebView上方添加一个视图使其看起来像边框?
我的快速解决方案.它涉及四个不同的功能,UIView的所有扩展.每个功能都添加不同的边框.
extension UIView { @discardableResult func addRightBorder(color: UIColor, width: CGFloat) -> UIView { let layer = CALayer() layer.borderColor = color.cgColor layer.borderWidth = width layer.frame = CGRect(x: self.frame.size.width-width, y: 0, width: width, height: self.frame.size.height) self.layer.addSublayer(layer) return self } @discardableResult func addLeftBorder(color: UIColor, width: CGFloat) -> UIView { let layer = CALayer() layer.borderColor = color.cgColor layer.borderWidth = width layer.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height) self.layer.addSublayer(layer) return self } @discardableResult func addTopBorder(color: UIColor, width: CGFloat) -> UIView { let layer = CALayer() layer.borderColor = color.cgColor layer.borderWidth = width layer.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: width) self.layer.addSublayer(layer) return self } @discardableResult func addBottomBorder(color: UIColor, width: CGFloat) -> UIView { let layer = CALayer() layer.borderColor = color.cgColor layer.borderWidth = width layer.frame = CGRect(x: 0, y: self.frame.size.height-width, width: self.frame.size.width, height: width) self.layer.addSublayer(layer) return self } }
这是快速的等价物
leftScrollView.clipsToBounds = true let rightBorder: CALayer = CALayer() rightBorder.borderColor = UIColor.darkGrayColor().CGColor rightBorder.borderWidth = 1 rightBorder.frame = CGRectMake(-1, -1, CGRectGetWidth(leftScrollView.frame), CGRectGetHeight(leftScrollView.frame)+2) leftScrollView.layer.addSublayer(rightBorder)