我想在UIViewController中绘制一条线而不使用代码,因此可以在情节提要板本身中给出这些线的约束。如果有办法,请告诉我。现在,我通过将UIView放在情节提要中来画一条线,并使用以下代码在其上画线。这是最好的方法吗?还是我有更好的方法?提前致谢。

class line: UIView {

    override func draw(_ rect: CGRect) {
        let aPath = UIBezierPath()

        aPath.move(to: CGPoint(x:0, y:1))

        aPath.addLine(to: CGPoint(x:500, y:1))

        //Keep using the method addLineToPoint until you get to the one where about to close the path

        aPath.close()

        //If you want to stroke it with a red color
        UIColor.blue.set()
        aPath.stroke()
        //If you want to fill it as well
        aPath.fill()
    }
}

使用Swift 3.0和Xcode 8.2

最佳答案

如下使用@IBDesignable。按照建议的高度设置视图,@ IBDesignable将在情节提要中显示您的台词:

import UIKit

@IBDesignable class line: UIView {

    override func draw(_ rect: CGRect) {
        let aPath = UIBezierPath()
        aPath.move(to: CGPoint(x: 0, y: 1))
        aPath.addLine(to: CGPoint(x: 250, y: 1))
        aPath.close()
        UIColor.blue.set()
        aPath.stroke()
    }
}

关于ios - iOS-有没有办法在不使用代码的情况下在UIViewController中画一条线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41736436/

10-08 20:58