Skip to content

Commit

Permalink
Fix repositionViews() infinite loop when using AutoLayout on a UITa…
Browse files Browse the repository at this point in the history
…bleViewCell (#47)

* Fix: use `intrinsicContentSize` for height calculations

* Example: TableViewController
  • Loading branch information
ricardopereira authored Jul 7, 2017
1 parent c3a9031 commit cd6201b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Example/WSTagsFieldExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
D73307521EB3B8DE00FD29B4 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73307511EB3B8DE00FD29B4 /* TableViewController.swift */; };
D7F33FF61D2A7378003F0082 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7F33FF51D2A7378003F0082 /* AppDelegate.swift */; };
D7F33FF81D2A7378003F0082 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7F33FF71D2A7378003F0082 /* ViewController.swift */; };
D7F33FFB1D2A7378003F0082 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D7F33FF91D2A7378003F0082 /* Main.storyboard */; };
Expand Down Expand Up @@ -55,6 +56,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
D73307511EB3B8DE00FD29B4 /* TableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
D7F33FF21D2A7378003F0082 /* WSTagsFieldExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WSTagsFieldExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
D7F33FF51D2A7378003F0082 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D7F33FF71D2A7378003F0082 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -99,6 +101,7 @@
children = (
D7F33FF51D2A7378003F0082 /* AppDelegate.swift */,
D7F33FF71D2A7378003F0082 /* ViewController.swift */,
D73307511EB3B8DE00FD29B4 /* TableViewController.swift */,
D7F33FF91D2A7378003F0082 /* Main.storyboard */,
D7F33FFC1D2A7378003F0082 /* Assets.xcassets */,
D7F33FFE1D2A7378003F0082 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -214,6 +217,7 @@
buildActionMask = 2147483647;
files = (
D7F33FF81D2A7378003F0082 /* ViewController.swift in Sources */,
D73307521EB3B8DE00FD29B4 /* TableViewController.swift in Sources */,
D7F33FF61D2A7378003F0082 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
5 changes: 4 additions & 1 deletion Example/WSTagsFieldExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = ViewController()
//window?.rootViewController = TableViewController()
return true
}

Expand Down
79 changes: 79 additions & 0 deletions Example/WSTagsFieldExample/TableViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// TableViewController.swift
// WSTagsFieldExample
//
// Created by Ricardo Pereira on 28/04/2017.
// Copyright © 2017 Whitesmith. All rights reserved.
//

import UIKit
import WSTagsField

class TableViewController: UITableViewController {

let records = ["Festival", "Salvador", "EuroVision", "Beer"]

override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
tableView.allowsSelection = false
tableView.register(TagsViewCell.self, forCellReuseIdentifier: String(describing: TagsViewCell.self))
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
tableView.reloadData()
//tableView.beginUpdates()
//tableView.endUpdates()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return records.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TagsViewCell.self), for: indexPath) as! TagsViewCell

cell.tagsField.onDidChangeHeightTo = { _ in
tableView.beginUpdates()
tableView.endUpdates()
}

return cell
}

}

class TagsViewCell: UITableViewCell {

let tagsField = WSTagsField()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
tagsField.placeholder = "Enter a tag"
tagsField.backgroundColor = .white
//tagsField.frame = CGRect(x: 0, y: 0, width: 300, height: 44)
tagsField.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(tagsField)

NSLayoutConstraint.activate([
tagsField.topAnchor.constraint(equalTo: contentView.topAnchor),
tagsField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
tagsField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
//tagsField.heightAnchor.constraint(greaterThanOrEqualToConstant: 44),
//tagsField.heightAnchor.constraint(equalToConstant: 44),
//tagsField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
contentView.bottomAnchor.constraint(equalTo: tagsField.bottomAnchor)
])

tagsField.addTag("alksjlkasd")
tagsField.addTag("alksjlkasd1")
tagsField.addTag("alksjlkasd2")
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

}
6 changes: 6 additions & 0 deletions Example/WSTagsFieldExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class ViewController: UIViewController {
readOnlyToggleButton.setTitle("Read Only", for: UIControlState())
view.addSubview(readOnlyToggleButton)
readOnlyToggleButton.addTarget(self, action: #selector(didTouchReadOnlyToggleButton), for: .touchUpInside)

//tagsField.addTag("Salvador Sobral")
//tagsField.addTag("EuroVision")
//tagsField.addTag("Portugal")
//tagsField.addTag("Lasdkjasop POAKSd jalskdj kajsld ka")
//tagsField.addTag("🇵🇹")
}

func didTouchTestButton(_ sender: AnyObject) {
Expand Down
4 changes: 2 additions & 2 deletions Source/WSTagView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ open class WSTagView: UIView {
}

open func sizeToFit(_ size: CGSize) -> CGSize {
if self.frame.size.width > size.width {
if intrinsicContentSize.width > size.width {
return CGSize(width: size.width, height: self.frame.size.height)
}
return self.frame.size
return intrinsicContentSize
}


Expand Down
12 changes: 9 additions & 3 deletions Source/WSTagsField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ open class WSTagsField: UIView {
selectedColor = .gray
selectedTextColor = .black

clipsToBounds = true

textField.backgroundColor = .clear
textField.autocorrectionType = UITextAutocorrectionType.no
textField.autocapitalizationType = UITextAutocapitalizationType.none
Expand Down Expand Up @@ -340,10 +342,14 @@ open class WSTagsField: UIView {
if let didChangeHeightToEvent = self.onDidChangeHeightTo {
didChangeHeightToEvent(self, newContentHeight)
}
frame.size.height = newContentHeight
if constraints.isEmpty {
frame.size.height = newContentHeight
}
}
else {
frame.size.height = oldContentHeight
else if frame.size.height != oldContentHeight {
if constraints.isEmpty {
frame.size.height = oldContentHeight
}
}
setNeedsDisplay()
}
Expand Down

0 comments on commit cd6201b

Please sign in to comment.