如何在 Swift 的分段控件中调整 textLabel 的大小?

How to resize textLabel in segmented control in Swift?

UIViewController 中,有一个分段控制器放置在视图的顶部。约束:左:0,right:0,上:0,height:90。

每次发布 UIApplicationDidBecomeActive 通知时,分段控件应通过调用 func updateUI()

用最新日期更新它的视图

首次加载时,当调用 viewDidLoad 时,每个段中的文本都按预期显示。
但是,如果应用程序进入后台,当它返回前台时,分段控件的标签不容纳文本并显示 3 个点...之后。请参阅下面的附件和 gif。

范围:每次发布 .UIApplicationDidBecomeActive 通知时更新每个段的分段控件标题。 Public Github 项目 https://github.com/bibscy/testProject

 class ViewController: UIViewController {

   var stringDates = [String]()

   var dateNow: Date {
     return Date()
   }
      @IBOutlet weak var segmentedControl: UISegmentedControl!

       func updateUI() {
    //create string dates and set the title of for each segment in segmented control
          self.getNextDays()  
     }

   override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: .UIApplicationDidBecomeActive, object: nil)

     UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0

      self.getNextDays()
   }
}


  extension ViewController {

//for each segment, construct a string date with the currentDate() being first, followed by 5 consecutive days
func getNextDays()  {

    stringDates.removeAll() //clear any previous data

    for i in 0...5 {
        let dateFormatter = DateFormatter()
        let today = dateNow
        let calendar = Calendar.current

        if i == 0 {
            let dayComponent = Calendar.current.component(.weekday,from: today)
            let day = Calendar.current.component(.day, from: today) //Int
            let dayString = dateFormatter.shortWeekdaySymbols[dayComponent-1] //Symbol


            let month = Calendar.current.component(.month, from: today)
            let monthSymbol = dateFormatter.shortMonthSymbols[month-1]
            let dayMonthString =  dayString + " " + String(day) + " " + monthSymbol
            stringDates.append(dayMonthString)


        } else {
            var components = DateComponents()
            components.weekday = i

            let nextDay = calendar.date(byAdding: components, to: today)
            let nextDayComponent = Calendar.current.component(.weekday,from: nextDay!)
            let day = Calendar.current.component(.day, from: nextDay!)
            let dayString = dateFormatter.shortWeekdaySymbols[nextDayComponent-1] // Symbol


            let month = Calendar.current.component(.month, from: nextDay!)
            let monthSymbol = dateFormatter.shortMonthSymbols[month-1]
            let dayMonthString = dayString + " " + String(day) + " " + monthSymbol
            stringDates.append(dayMonthString)
        }
    }


      //set the title for each segment from stringDates array
       for value in 0...5 {
         self.segmentedControl.setTitle(self.stringDates[value], forSegmentAt: value)
      }
   } //end of getNextDays()  
}

该问题仅在 10.3 iOS 版本中重现。 解决方案:

存储分段标签的初始属性:

fileprivate var height: CGFloat! 
fileprivate var width: CGFloat!

getNextDays:

之前添加到 ViewDidLoad
let label = segmentedControl.subviews[0].subviews[0] as! UILabel 
height = label.frame.height 
width = label.frame.width

设置文字:

for value in 0...5 {
   segmentedControl.setTitle(stringDates[value], forSegmentAt: value)
}

然后修复帧:

let labels = segmentedControl.subviews.map{ [=13=].subviews[1] } as! [UILabel]
    labels.forEach { (label) in
        label.numberOfLines = 0
        let frame = label.frame
        label.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: width, height: height)
    }

Sample code with some crash protection