在 Swift 中更改 UIProgressView(Bar 样式)的高度

Change height of UIProgressView (Bar style) in Swift

我在 Swift 2.2 中创建了一个 UIProgressView(条形),现在想改变它的高度。其他一些帖子建议使用 CGAffineTransformScale 来更改高度。但是,出于某种原因,这种方法似乎不起作用,因为它对 UIProgressView 的高度没有任何影响。

下面是我的代码:

let progressBar = UIProgressView(progressViewStyle: .Bar)
progressBar.progress = 0.5
progressBar.translatesAutoresizingMaskIntoConstraints = false
progressBar.transform = CGAffineTransformScale(progressBar.transform, 1, 20)
self.navigationItem.titleView = progressBar

感谢您的帮助,非常感谢!

您可以子类化 UIProgressView,并添加 height 属性:

结果:

import UIKit

class CustomProgressView: UIProgressView {

    var height:CGFloat = 1.0 
    // Do not change this default value, 
    // this will create a bug where your progressview wont work for the first x amount of pixel. 
    // x being the value you put here.

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        let size:CGSize = CGSize.init(width: self.frame.size.width, height: height)

        return size
    }

}

vc 中,您初始化 customProgressvView,并设置 height:

import UIKit

class ViewController2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let progressBar = CustomProgressView(progressViewStyle: .bar)
        progressBar.progress = 0.5
        progressBar.height = 10.0
        self.navigationItem.titleView = progressBar

    }
}