我正在尝试添加一个功能,该功能将在我的自定义警报视图中显示关闭按钮。 当我第一次调用它时,只会出现一个不包含按钮的空白区域(没有颜色,没有文本并且没有按下)。再次致电时-一切正常。
它不依赖于参数animated
,在两种情况下它都是一样的。
我的dialogView
是我的自定义UIView
。我不使用UIAlertController
可能是什么问题?
initial view after the first attempt second attempt
GIF call func by click
func showCloseButton(text: String, animated: Bool){
let dialogViewHeight = self.dialogView.frame.height + 50 + 1
let button = UIButton(type: .roundedRect)
button.backgroundColor = .red
button.frame.origin = CGPoint(x: 0, y: dialogViewHeight)
button.frame.size = CGSize(width: self.dialogView.frame.width, height: 50)
button.setTitle(text, for: .normal)
button.addTarget(self, action: #selector(closeTapped), for: .touchUpInside)
let separatorLineView = UIView()
separatorLineView.frame.origin = CGPoint(x: 0, y: self.dialogView.frame.height)
separatorLineView.frame.size = CGSize(width: dialogView.frame.width, height: 1)
separatorLineView.backgroundColor = UIColor.groupTableViewBackground
dialogView.addSubview(separatorLineView)
if animated {
animateAdjustDialogView(height: dialogViewHeight){
Logger.Log("completion did")
self.dialogView.addSubview(button)
}
} else {
Logger.Log("button.frame.origin = \(button.frame.origin)")
Logger.Log("button.frame.size = \(button.frame.size)")
Logger.Log("button.title = \(button.titleLabel)")
self.dialogView.frame.size = CGSize(width: self.dialogView.frame.width, height: dialogViewHeight)
self.dialogView.addSubview(button)
}
}
private func animateAdjustDialogView(height: CGFloat, completion: (()->Void)?){
UIView.animate(withDuration: 1.0, animations: {
self.dialogView.frame.size = CGSize(width: self.dialogView.frame.width, height: height)
self.layoutIfNeeded()
}) { (finished) in
Logger.Log("finished = \(finished)")
if finished {
Logger.Log("completion run")
completion?()
}
}
}
答案 0 :(得分:1)
按钮框架引起了问题-
button.frame.origin = CGPoint(x: 0, y: dialogViewHeight)
位置
let dialogViewHeight = self.dialogView.frame.height + 50 + 1
这意味着button
超出了dialogView
frame
。
因此替换
button.frame.origin = CGPoint(x: 0, y: dialogViewHeight)
使用
button.frame.origin = CGPoint(x: 0, y: dialogViewHeight - 50) // Height of the Button.
让我知道您是否仍然有任何问题。