滚动视图 iPhone 周围的阴影或边框
我想在 uiscrollview 周围绘制边框/阴影,我知道我可以通过附加视图或滚动视图到达那里,但不喜欢处理缺点,但我听说应该有可能直接在滚动视图上绘制边框这就是我想要的。
我对 iPhone 开发很陌生,任何答案都会有帮助。
i would like to draw a border / shadow around a uiscrollview, i know that i could get there with an additional view or scrollview but dont like the handling an drawbacks but i heard that there should be a possibility to dirctly draw a border to a scrollview and that is what i would prefer.
I am quiet new to iphone developement,any answer would helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用滚动视图(或任何 UIView)的 Layer 属性,您可以轻松获得实心边框...
您还可以通过设置
layer.shadow*
属性,但使用此技术性能可能会很慢,因此我通常更喜欢使用以下更复杂但性能更高的技术。您可以创建中间透明、边缘周围有阴影的 PNG 图像 - 它需要有 9 个不同的区域:每个角 4 个,每个边缘 4 个,中间有一个完全透明的 1x1 像素区域。例如,如果您的阴影将 6 像素延伸到图像中,则您的图像将为 13x13,具有 6 像素宽/高边框和中间的 1x1 区域。然后使用以下方法将其设置为可缩放图像:更新: 由于 iOS 5.0
stretchableImageWithLeftCapWidth:topCapHeight:
已弃用,因此仅当您仍想支持 iOS 4.x 设备时才使用此选项。如果您只想支持 iOS 5.0+ 设备,请使用以下方法:然后将图像放在父视图上,使其占据滚动视图的整个区域。如果您希望阴影覆盖可滚动元素(因此您的滚动视图看起来嵌入/位于页面其余部分的后面),则在顶部放置一个透明的 UIView,上面有阴影图像,以便它显示到您的滚动视图在它后面。
If you use the layer property of your scroll view (or any UIView) you can easily get a solid border...
You can also use the layer to apply real-time shadows by setting the
layer.shadow*
properties, but performance can be slow with this technique, so I generally prefer to use the following more complex, but more performant technique. You can create a PNG image with transparency in the middle and shadows around the edge - it needs to have 9 distinct areas: 4 for each corner, 4 for each edge, and a completely transparent 1x1 pixel area in the middle. For example if your shadow extends 6 pixels into your image, your image would be 13x13 with the 6 pixel wide/high borders and the 1x1 area in the middle. Then you set it as a scalable image using:UPDATE: Since iOS 5.0
stretchableImageWithLeftCapWidth:topCapHeight:
is deprecated so only use this if you still want to support iOS 4.x devices. If you want to support only iOS 5.0+ devices use this instead:Then you put the image on the parent view so it takes up the entire area of the scroll view. If you want the shadows to go OVER your scrollable elements, (so your scroll view looks inset/behind the rest of the page) then place a transparent UIView over the top with the shadow image on it so that it shows through to your scroll view behind it.
不要忘记:
如果您想使用
Dont forget to:
If you want to use
要从 UIColor 获取 CGColorRef,您还可以使用以下示例:
To get the CGColorRef from an UIColor you can also use this example:
myView.layout.borderColor 在使用 UIColor 时生成不兼容的指针警告,并且没有为我更改边框。
使用 CGColorRef 对我有用:
myView.layout.borderColor generates an incompatible pointer warning when using UIColor, and did not change the border for me.
Using CGColorRef worked for me instead: