xcode 如何在 Swift 中弯曲 UIViewController 的顶部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37153535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:48:23  来源:igfitidea点击:

How to curve the top of a UIViewController in Swift?

xcodeswiftuiviewcontroller

提问by Doughnut Man

I'm trying to figure out how to curve just the top of the UIViewController using Swift.

我试图弄清楚如何使用 Swift 仅弯曲 UIViewController 的顶部。

enter image description here

在此处输入图片说明

I want it curved like it shows in this photo.

我希望它像这张照片中显示的那样弯曲。

回答by Moriya

You could draw the background with some bezier curves which shouldn't be too hard to do. You especially want to do this if you want to recreate something like into the bottom of that example as well.

你可以用一些贝塞尔曲线来绘制背景,这应该不会太难。如果您还想在该示例的底部重新创建类似的内容,您尤其想这样做。

Here's a custom view you can use. it just overrides the drawing and makes its background with a bezier path that has a rounded top

这是您可以使用的自定义视图。它只是覆盖绘图并使用具有圆顶的贝塞尔曲线路径使其背景

You can just adjust the height and curve with the static values I inserted.

您可以使用我插入的静态值调整高度和曲线。

class CurvedView: UIView {
    override func drawRect(rect: CGRect) {

        let y:CGFloat = 20
        let curveTo:CGFloat = 0

        let myBezier = UIBezierPath()
        myBezier.moveToPoint(CGPoint(x: 0, y: y))
        myBezier.addQuadCurveToPoint(CGPoint(x: rect.width, y: y), controlPoint: CGPoint(x: rect.width / 2, y: curveTo))
        myBezier.addLineToPoint(CGPoint(x: rect.width, y: rect.height))
        myBezier.addLineToPoint(CGPoint(x: 0, y: rect.height))
        myBezier.closePath()
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 4.0)
        UIColor.whiteColor().setFill()
        myBezier.fill()
    }
}

Another way would be to just make a white view that is much bigger than the screen. I'm guessing 3 times the screen width. Then just set the corner radius to half of its width making it round.

另一种方法是制作一个比屏幕大得多的白色视图。我猜是屏幕宽度的 3 倍。然后只需将角半径设置为其宽度的一半,使其变圆。

回答by A.G

To have rounded cornered edges of View:

有圆角的视图边缘:

In case of UIViewController's default view:

在 UIViewController 的默认视图的情况下:

override func viewWillAppear(animated: Bool) {

    self.navigationController?.navigationBarHidden =  true

    //UIView Corner Radius
    self.view.layer.cornerRadius = 20.0;
    self.view.layer.masksToBounds = true

    //UIView Set up border
    self.view.layer.borderColor = UIColor.yellowColor().CGColor;
    self.view.layer.borderWidth = 3.0;        
}

In case of the top status bar:

如果是顶部状态栏:

Make a custom View as the top status bar.

将自定义视图作为顶部状态栏。

func addStatusBar()
{
    let statusBarView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))

    statusBarView.backgroundColor = UIColor.greenColor()

    //UIView Corner Radius
    statusBarView.layer.cornerRadius = 5.0;
    statusBarView.layer.masksToBounds = true

    //UIView Set up border
    statusBarView.layer.borderColor = UIColor.yellowColor().CGColor;
    statusBarView.layer.borderWidth = 3.0;

    self.navigationController?.navigationBarHidden =  true
    self.view.addSubview(statusBarView)
}

Adding the custom status bar to view:

添加自定义状态栏查看:

override func viewWillAppear(animated: Bool) {
    self.addStatusBar()
}

To make a view having rounded top:

要制作圆顶视图:

extension UIView {

    func addTopRoundedCornerToView(targetView:UIView?, desiredCurve:CGFloat?)
    {
        let offset:CGFloat =  targetView!.frame.width/desiredCurve!
        let bounds: CGRect = targetView!.bounds

        let rectBounds: CGRect = CGRectMake(bounds.origin.x, bounds.origin.y+bounds.size.height / 2, bounds.size.width, bounds.size.height / 2)

        let rectPath: UIBezierPath = UIBezierPath(rect: rectBounds)
        let ovalBounds: CGRect = CGRectMake(bounds.origin.x - offset / 2, bounds.origin.y, bounds.size.width + offset, bounds.size.height)
        let ovalPath: UIBezierPath = UIBezierPath(ovalInRect: ovalBounds)
        rectPath.appendPath(ovalPath)

        // Create the shape layer and set its path
        let maskLayer: CAShapeLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = rectPath.CGPath

        // Set the newly created shape layer as the mask for the view's layer
        targetView!.layer.mask = maskLayer
    }
}

Usage:

用法:

override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden =  true
    self.view.addTopRoundedCornerToView(self.view, desiredCurve: 0.6)
}