Как центр UILabel в TodayExtension программно - PullRequest
0 голосов
/ 05 октября 2019

Я ищу способ центрировать UILabel в виджете TodayExtension. UILabel должна быть центрирована в режиме .compact , и даже когда пользователь расширяет виджет, UILabel должен NOT изменить свою позицию.

// UILabel programmatically
let location: UILabel = {
    let location = UILabel()
    location.text = ""
    location.textColor = UIColor.black
    location.font = UIFont.systemFont(ofSize: 45, weight: UIFont.Weight.thin)
    location.adjustsFontSizeToFitWidth = true
    location.minimumScaleFactor = 0.5
    location.numberOfLines = 0
    return location
}()

// Constraints
currentPrayerTime.translatesAutoresizingMaskIntoConstraints = false
currentPrayerTime.heightAnchor.constraint(equalToConstant: 55).isActive = true
currentPrayerTime.leftAnchor.constraint(greaterThanOrEqualTo: otherLabel.rightAnchor, constant: 10).isActive = true
currentPrayerTime.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
currentPrayerTime.rightAnchor.constraint(lessThanOrEqualTo: view.rightAnchor, constant: -10).isActive = true

код работает, но проблема здесь в том, что UILabel меняет свою y-позицию, когда я раскрываю виджет. Возможным решением может быть создание UIView с фиксированной высотой и добавление UILabel в качестве подпредставления. Но я думаю, что это решение не совсем чистое.

...