在 a 中UIViewController
有一个 Segmented Controller 放置在视图的顶部。约束:左:0,右:0,顶:0,高度:90。
每次UIApplicationDidBecomeActive
发布通知时,分段控件应通过调用以最新日期更新其视图func updateUI()
在第一次加载时,当viewDidLoad
被调用时,每个段中的文本按预期显示。
但是,如果应用程序进入后台,当它回到前台时,分段控件的标签不容纳文本并显示 3 个点......之后。请参阅下面的附件和 gif。
范围:每次.UIApplicationDidBecomeActive
发布通知时更新每个段的分段控件标题。公共 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!
添加到 ViewDidLoad 之前getNextDays
:
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{ $0.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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句