带圆角的 UIToolbar
我想显示一个顶部带有圆角的 UIToolbar
,最简单的方法是什么?工具栏未在窗口上对齐;它周围都有边距。谢谢!
I want to display a UIToolbar
with rounded corners on top, what would be the easiest way? the toolbar is not top-aligned on the window; it has a margin all around. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很简单。
首先 - 在视图控制器的 .h 文件中有一个 UIToolbar 的 IBOutlet 变量。
例如。
现在,在视图控制器文件的 .m 文件中,只需放置以下代码 &它会对你产生魔力。但是,如果有任何疑问,请添加评论。
Very simple.
First of all - have an IBOutlet variable of UIToolbar in your .h file of your view controller.
For example.
Now in your .m file of your view controller file just place following code & it will work as a magic for you. However- please add comment if any queries.
圆化视图角的最简单方法是使用
CALayer
的cornerRadius
(和masksToBounds
)属性。但是,这样您只能选择将所有角均等地圆化。要使用该属性,您可以将UIToolbar
放入另一个比工具栏高的视图中,这样只有顶部是圆形的。如果另一个视图有圆角底角,这会很有效。将视图屏蔽为任意形状的最简单方法是将
CALayer
的mask
属性设置为新的CAShapeLayer
。在您的情况下,使用CGPathAddLineToPoint
和CGPathAddArcToPoint
或类似方法为CAShapeLayer
构建CGPath
,以仅获取顶角圆形。The easiest way to round the corners of a view is with the
cornerRadius
(andmasksToBounds
) property ofCALayer
. However, with that you only have the option of rounding all the corners equally. To use that property, you could put theUIToolbar
into another view that was taller than the toolbar, so only the top was rounded. This would work well if another view will have rounded bottom corners.The easiest way to mask a view to an arbitrary shape is to set the
mask
property ofCALayer
to a newCAShapeLayer
. In your case, build aCGPath
for theCAShapeLayer
usingCGPathAddLineToPoint
andCGPathAddArcToPoint
or similar to get only the top corners rounded.