From 0dff815e6e7b665b949f6f85c54b07270726b0ec Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 7 Oct 2017 00:16:13 +0800 Subject: [PATCH 1/2] Fix the layout of the example app in iOS 11 --- Example/CustomizedTokenField.swift | 4 ++++ Example/CustomizedTokenViewController.swift | 15 ++++++++++++++- Example/ExampleViewController.swift | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Example/CustomizedTokenField.swift b/Example/CustomizedTokenField.swift index b79d4fb..c8c1870 100644 --- a/Example/CustomizedTokenField.swift +++ b/Example/CustomizedTokenField.swift @@ -39,6 +39,10 @@ class CustomizedTokenField: ICTokenField { applyCustomizedStyle() } + override var intrinsicContentSize: CGSize { + return UILayoutFittingExpandedSize + } + } diff --git a/Example/CustomizedTokenViewController.swift b/Example/CustomizedTokenViewController.swift index b167724..fc5e5cf 100644 --- a/Example/CustomizedTokenViewController.swift +++ b/Example/CustomizedTokenViewController.swift @@ -56,7 +56,20 @@ class CustomizedTokenViewController: UIViewController, ICTokenFieldDelegate { cancelBarButton.tintColor = UIColor.white navigationItem.rightBarButtonItem = cancelBarButton - navigationItem.titleView = tokenField + navigationItem.titleView = { + if #available(iOS 11, *) { + tokenField.translatesAutoresizingMaskIntoConstraints = false + let views = ["field": tokenField] + let metrics = ["padding": 7] + let containerView = UIView() + containerView.addSubview(tokenField) + containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[field]-padding-|", options: [], metrics: metrics, views: views)) + containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-padding-[field]-padding-|", options: [], metrics: metrics, views: views)) + return containerView + } else { + return tokenField + } + }() tokenField.delegate = self } diff --git a/Example/ExampleViewController.swift b/Example/ExampleViewController.swift index fcbe458..e4dbd58 100644 --- a/Example/ExampleViewController.swift +++ b/Example/ExampleViewController.swift @@ -54,6 +54,7 @@ class ExampleViewController: UITableViewController { override func loadView() { super.loadView() + tableView.rowHeight = 44 tableView.register(ExampleCell.self, forCellReuseIdentifier: String(describing: ExampleCell.self)) tableView.tableFooterView = flipButton tableView.tableFooterView?.isUserInteractionEnabled = true From 2ea8817825cdfe229979b24f0d92bfc196d7220a Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 27 Oct 2017 17:07:24 +0800 Subject: [PATCH 2/2] Manually notify the delegate of the changed input text when the delete key is pressed --- Source/TokenField/ICTokenField.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Source/TokenField/ICTokenField.swift b/Source/TokenField/ICTokenField.swift index 2c51980..989cb29 100644 --- a/Source/TokenField/ICTokenField.swift +++ b/Source/TokenField/ICTokenField.swift @@ -328,19 +328,25 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega // MARK: - ICBackspaceTextFieldDelegate @nonobjc func textFieldShouldDelete(_ textField: ICBackspaceTextField) -> Bool { - if tokens.isEmpty { + if !textField.showsCursor { + _ = removeHighlightedToken() return true } - if !textField.showsCursor { - _ = removeHighlightedToken() + guard let text = textField.text else { return true } - if let text = textField.text, text.isEmpty { + if text.isEmpty { textField.showsCursor = false tokens.last?.isHighlighted = true + } else { + // textField(_:shouldChangeCharactersIn:replacementString:) is skipped when the delete key is pressed. + // Notify the delegate of the changed input text manually. + let index = text.index(text.endIndex, offsetBy: -1) + delegate?.tokenField?(self, didChangeInputText: text.substring(to: index)) } + return true }