Skip to content

Commit

Permalink
Fixed a crash when user backgrounded app to change the preferred font…
Browse files Browse the repository at this point in the history
… size of the app. This only happens if the app uses `preferredFont:forTextStyle`. So no need to worry if you were using other fonts.
  • Loading branch information
mats-claassen authored and Martin Barreto committed Aug 12, 2016
1 parent e25cbb0 commit 6af3758
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Example/Example/CustomCells.swift
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ public final class EmailFloatLabelRow: FloatFieldRow<EmailFloatLabelCell>, RowTy
public final class LocationRow : SelectorRow<PushSelectorCell<CLLocation>, MapViewController>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
presentationMode = .show(controllerProvider: ControllerProvider.callback { return MapViewController(){ _ in } }, completionCallback: { vc in vc.navigationController?.popViewController(animated: true) })
presentationMode = .show(controllerProvider: ControllerProvider.callback { return MapViewController(){ _ in } }, completionCallback: { vc in _ = vc.navigationController?.popViewController(animated: true) })

displayValueFor = {
guard let location = $0 else { return "" }
let fmt = NumberFormatter()
Expand Down
14 changes: 14 additions & 0 deletions Source/Rows/Common/FieldRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ public class _FieldCell<T where T: Equatable, T: InputTypeInitiable> : Cell<T>,

public required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillResignActive, object: nil, queue: nil){ [weak self] notification in
guard let me = self else { return }
me.titleLabel?.removeObserver(me, forKeyPath: "text")
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationDidBecomeActive, object: nil, queue: nil){ [weak self] notification in
self?.titleLabel?.addObserver(self!, forKeyPath: "text", options: NSKeyValueObservingOptions.old.union(.new), context: nil)
}

NotificationCenter.default.addObserver(forName: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil, queue: nil){ [weak self] notification in
self?.setNeedsUpdateConstraints()
}
}

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -151,6 +162,9 @@ public class _FieldCell<T where T: Equatable, T: InputTypeInitiable> : Cell<T>,
textField.removeTarget(self, action: nil, for: .allEvents)
titleLabel?.removeObserver(self, forKeyPath: "text")
imageView?.removeObserver(self, forKeyPath: "image")
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
}

public override func setup() {
Expand Down
14 changes: 14 additions & 0 deletions Source/Rows/PostalAddressRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ public class PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalA

public required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillResignActive, object: nil, queue: nil){ [weak self] notification in
guard let me = self else { return }
me.titleLabel?.removeObserver(me, forKeyPath: "text")
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationDidBecomeActive, object: nil, queue: nil){ [weak self] notification in
self?.titleLabel?.addObserver(self!, forKeyPath: "text", options: NSKeyValueObservingOptions.old.union(.new), context: nil)
}

NotificationCenter.default.addObserver(forName: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil, queue: nil){ [weak self] notification in
self?.setNeedsUpdateConstraints()
}
}

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -174,6 +185,9 @@ public class PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalA
countryTextField.removeTarget(self, action: nil, for: .allEvents)
titleLabel?.removeObserver(self, forKeyPath: "text")
imageView?.removeObserver(self, forKeyPath: "image")
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
}

public override func setup() {
Expand Down
14 changes: 14 additions & 0 deletions Source/Rows/SegmentedRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public class SegmentedCell<T: Equatable> : Cell<T>, CellType {

required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillResignActive, object: nil, queue: nil){ [weak self] notification in
guard let me = self else { return }
me.titleLabel?.removeObserver(me, forKeyPath: "text")
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationDidBecomeActive, object: nil, queue: nil){ [weak self] notification in
self?.titleLabel?.addObserver(self!, forKeyPath: "text", options: NSKeyValueObservingOptions.old.union(.new), context: nil)
}

NotificationCenter.default.addObserver(forName: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil, queue: nil){ [weak self] notification in
self?.setNeedsUpdateConstraints()
}
}

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -37,6 +48,9 @@ public class SegmentedCell<T: Equatable> : Cell<T>, CellType {
segmentedControl.removeTarget(self, action: nil, for: .allEvents)
titleLabel?.removeObserver(self, forKeyPath: "text")
imageView?.removeObserver(self, forKeyPath: "image")
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
}

public override func setup() {
Expand Down
41 changes: 29 additions & 12 deletions Source/Rows/SliderRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public class SliderCell: Cell<Float>, CellType {

public required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil, queue: nil){ [weak self] notification in
guard let me = self else { return }
if me.shouldShowTitle() {
me.contentView.addSubview(me.titleLabel)
me.contentView.addSubview(me.valueLabel!)
me.addConstraints()
}
}
}

deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
}

required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -65,18 +77,7 @@ public class SliderCell: Cell<Float>, CellType {
contentView.addSubview(valueLabel!)
}
contentView.addSubview(slider)

let views = ["titleLabel" : titleLabel, "valueLabel" : valueLabel, "slider" : slider]
let metrics = ["hPadding" : 16.0, "vPadding" : 12.0, "spacing" : 12.0]

if shouldShowTitle() {
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-hPadding-[titleLabel]-[valueLabel]-hPadding-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: metrics, views: views))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-vPadding-[titleLabel]-spacing-[slider]-vPadding-|", options: NSLayoutFormatOptions.alignAllLeft, metrics: metrics, views: views))

} else {
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-vPadding-[slider]-vPadding-|", options: NSLayoutFormatOptions.alignAllLeft, metrics: metrics, views: views))
}
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-hPadding-[slider]-hPadding-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: metrics, views: views))
addConstraints()
}

public override func update() {
Expand All @@ -88,6 +89,22 @@ public class SliderCell: Cell<Float>, CellType {
slider.value = row.value ?? 0.0
}

func addConstraints(justLabelConstraints: Bool = false) {

This comment has been minimized.

Copy link
@mtnbarreto

mtnbarreto Jan 30, 2017

Member

@mats-claassen Are we using justLabelConstraints parameter in any custom row? base Eureka code does not use it.

This comment has been minimized.

Copy link
@mats-claassen

mats-claassen Jan 31, 2017

Author Member

No, I do not think so

let views = ["titleLabel" : titleLabel, "valueLabel" : valueLabel, "slider" : slider]
//TODO: in Iphone 6 Plus hPadding should be 20
let metrics = ["hPadding" : 15.0, "vPadding" : 12.0, "spacing" : 12.0]
if shouldShowTitle() {
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-hPadding-[titleLabel]-[valueLabel]-hPadding-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: metrics, views: views))
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-vPadding-[titleLabel]-spacing-[slider]-vPadding-|", options: NSLayoutFormatOptions.alignAllLeft, metrics: metrics, views: views))

} else if !justLabelConstraints {
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-vPadding-[slider]-vPadding-|", options: NSLayoutFormatOptions.alignAllLeft, metrics: metrics, views: views))
}
if !justLabelConstraints {
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-hPadding-[slider]-hPadding-|", options: NSLayoutFormatOptions.alignAllLastBaseline, metrics: metrics, views: views))
}
}

func valueChanged() {
let roundedValue: Float
let steps = Float(sliderRow.steps)
Expand Down

0 comments on commit 6af3758

Please sign in to comment.