创建具有圆形顶部边缘的UIView [英] Create a UIView with rounded top edge

查看:69
本文介绍了创建具有圆形顶部边缘的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个具有圆形顶部边缘的UIView,如图所示,该怎么办?

I would like to create an UIView with rounded top edge like this image, how can I do it please?

想要的结果

不需要的结果

推荐答案

要重新发布我在其他主题上发布的答案:

To repost an answer I posted on a different thread:

我现在可以确认这是iOS 6之后引入的错误.我有一个运行iOS 6.1的旧4s.在那台机器上,此代码:

I can now confirm that this is a bug introduced after iOS 6. I have an old 4s running iOS 6.1. On that machine, this code:

  path = [UIBezierPath bezierPathWithRoundedRect: bounds
    byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight
   cornerRadii: CGSizeMake(bounds.size.width/2, bounds.size.width/6)
 ];

创建一个圆角矩形,其圆角为椭圆形.如您所愿,曲线在曲线的顶部更加平缓,在侧面更加锐利:

Creates a rounded rectangle with the corners oval-shaped. The curve is much more gradual on the top part of the curve, and much sharper on the sides, as you would expect:

这是iOS 6.1图像,其角应为:

This is the iOS 6.1 image, with the corners as they should be:

这是从iOS 8.1.2运行时相同的代码:

And here is what the same code looks like when run from iOS 8.1.2:

在iOS> = 7.0上,它似乎忽略了指定半径的高度,并且对角椭圆的高度和宽度都使用了宽度值,这迫使它们始终为四分之一圆.

It appears that on iOS >=7.0, it ignores the height of the specified radius and uses the width value for both the height and the width of the corner ovals, which forces them to always be quarter circles.

我已经在Apple的错误报告器系统上记录了一个错误.我们将看看他们怎么说. 我建议其他所有看到此问题的人也报告一个错误.他们获得的报告越多,他们修复它的可能性就越大.

I've logged a bug on apple's bug reporter system. We'll see what they say. I suggest everybody else who's seeing this problem report a bug also. The more reports they get, the more likely they are to fix it.

(忽略底部的UISwitch.这是先前测试遗留下来的.)

(ignore the UISwitch at the bottom. That's left over from a previous test.)

我已经编写了构建bezier曲线的代码,该曲线大致近似于由iOS< 7.我刚刚找到了关于带有贝塞尔曲线的近似圆的 文章 .使用该文章中的控制点,构建适用于所有版本iOS的bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:方法并不困难.

I've written code that builds a bezier curve that roughly approximates the look of the curve generated by iOS < 7. I just found an article on approximating circles with bezier curves. Using the control points from that article it wouldn't be that hard to build our own equivalent of the bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: method that works on all versions of iOS.

这篇关于创建具有圆形顶部边缘的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆