Created
October 21, 2019 22:58
-
-
Save anzfactory/e5c79bb95e11b3f4c5ab37bfa158a98b to your computer and use it in GitHub Desktop.
リロードしたときにテキストが下にずれ込むっていう話
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
class MyViewController : UIViewController { | |
private let container: UIView = { | |
let view = UIView(frame: .zero) | |
view.backgroundColor = .white | |
return view | |
}() | |
private let tableView: UITableView = { | |
let tableView = UITableView(frame: .zero, style: .plain) | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
tableView.register(TableViewCell.self, forCellReuseIdentifier: "CELL") | |
return tableView | |
}() | |
override func loadView() { | |
tableView.dataSource = self | |
container.addSubview(tableView) | |
NSLayoutConstraint.activate([ | |
tableView.topAnchor.constraint(equalTo: container.topAnchor), | |
tableView.leftAnchor.constraint(equalTo: container.leftAnchor), | |
tableView.rightAnchor.constraint(equalTo: container.rightAnchor), | |
tableView.bottomAnchor.constraint(equalTo: container.bottomAnchor) | |
]) | |
self.view = container | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
// なにか遅延処理はしってその結果更新する | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { | |
self.tableView.reloadData() | |
} | |
} | |
} | |
extension MyViewController: UITableViewDataSource { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 100 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "CELL", for: indexPath) as! TableViewCell | |
cell.configure() | |
return cell | |
} | |
} | |
class TableViewCell: UITableViewCell { | |
private let attributes: [NSAttributedString.Key: Any] = { | |
let paragraph = NSMutableParagraphStyle() | |
paragraph.minimumLineHeight = 22 | |
paragraph.maximumLineHeight = 22 | |
return [ | |
.paragraphStyle: paragraph, | |
.font: UIFont.systemFont(ofSize: 12.0) | |
] | |
}() | |
override func prepareForReuse() { | |
super.prepareForReuse() | |
textLabel?.text = "" | |
} | |
func configure() { | |
textLabel?.attributedText = NSAttributedString( | |
string: "にゃーん", | |
attributes: attributes | |
) | |
} | |
} | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment