앱을 만들다 도중 발생한 쓰레드 1 error ..
앵커로 제약조건을 활성화가 되지 않는다라...
일단 대충 layout 관련인 에러인지는 알겠는데,,,
마지막에 보이는 !! dropbutton 에 관한 거구나..라는것도!! 알겠는데...
굉장히 당황스러운건...?
이런식으로 첫 쓰레드 때는 잘작동이 된다는것!!
근데 크러쉬가 취소!! 즉 제가 뷰를 dismiss 시킬때 문제가 되는 것인데유!!
보통 이런문제는 LayoutConstraints 가 잡히고 view에 addSubview 를 하는 경우
발생 된다고들 하는데......
snapkit으로 잡았던 레이아웃도 NS로 잡아봤지만 그 방법도 크러쉬...
dropbutton자체를 라이브러리 사용하지 않고 만들어서 써서 그런가..라는..
생각도 들고...
import UIKit
import SnapKit
// Upload에서 쓸 드롭버튼
// 라이브러리 쓸까하다가 만들어서 사용하기로
class dropButton : UIButton, dropProtocol {
func didTapDropButton(string: String) {
self.setTitle(string, for: .normal)
self.dismissButton() //선택되면 dismiss
}
var drop = dropView()
// Snapkit 으로 layout 설정시 변하는 layout을 사용할때씀
var heightConstant : Constraint? = nil
override init(frame: CGRect) {
super.init(frame: frame)
drop.layer.cornerRadius = 10
drop = dropView.init(frame: .zero)
drop.translatesAutoresizingMaskIntoConstraints = false
drop.delegate = self //연결!! 필수 쫌하자!!
}
override func didMoveToSuperview() {
self.superview?.addSubview(drop)
self.superview?.bringSubviewToFront(drop)
drop.snp.makeConstraints { (make) in
make.top.equalTo(self.snp.bottom)
make.centerX.equalTo(self.snp.centerX)
make.width.equalTo(self.snp.width)
heightConstant = make.height.equalTo(0).constraint
}
}
var openValue = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if openValue == false {
openValue = true
if self.drop.tableView.contentSize.height > 80 {
self.heightConstant?.update(offset: 80)
}else{
self.heightConstant?.update(offset: self.drop.tableView.contentSize.height)
}
UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveEaseOut, animations: {
self.drop.layoutIfNeeded()
self.drop.center.y += self.drop.frame.height / 2
}, completion: nil)
} else{
openValue = false
self.heightConstant?.update(offset: 0)
UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveEaseIn, animations: {
self.drop.center.y -= self.drop.frame.height / 2
self.drop.layoutIfNeeded()
}, completion: nil)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func dismissButton(){
openValue = false
self.heightConstant?.update(offset: 0)
UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveEaseIn, animations: {
self.drop.center.y -= self.drop.frame.height / 5
self.drop.layoutIfNeeded()
}, completion: nil)
}
}
class dropView : UIView , UITableViewDelegate , UITableViewDataSource{
var option = [String]()
var tableView = UITableView()
var delegate : dropProtocol!
override init(frame: CGRect) {
super.init(frame: frame)
tableView.delegate = self
tableView.dataSource = self
tableView.layer.cornerRadius = 10
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = .secondaryLabel
self.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.left.equalToSuperview()
make.right.equalToSuperview()
make.bottom.equalToSuperview()
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return option.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = option[indexPath.row]
cell.textLabel?.font = .systemFont(ofSize: 10, weight: .bold)
cell.textLabel?.textColor = .secondarySystemBackground
cell.backgroundColor = .secondaryLabel
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate.didTapDropButton(string: option[indexPath.row])
self.tableView.deselectRow(at: indexPath, animated: true)
}
}
protocol dropProtocol {
func didTapDropButton(string:String)
}
흠..일단 이건 버리고 그냥 드롭버튼 라이브러리를 사용해야겠다..ㅎㅎ