ios - 为什么按钮仅在第二次尝试时出现?
问题描述
我正在尝试添加一个功能,该功能将在我的自定义警报视图中显示关闭按钮。当我第一次调用它时,只出现一个不包含按钮的空白区域(没有颜色,没有文本,也没有按下)。当您再次致电时 - 一切正常。
它不依赖于参数animated
,在这两种情况下它的工作原理都是一样的。
我dialogView
是我的习惯UIView
。我不使用UIAlertController
可能是什么问题呢?
第一次尝试后的 初始视图第二次尝试
通过单击调用 GIF 函数
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?()
}
}
}
解决方案
按钮框架导致问题 -
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.
如果您仍有任何问题,请告诉我。
推荐阅读
- c# - 实验和unity3D(协程、定时器和具体条件)
- java - 处理 tcp java socket 项目中的异常
- c# - 如何有一个带有项目的 UserControl 和一个我想通过 XAML 代码中的属性插入另一个 UserControl 的 contentControl?
- spring - 无法注册在类路径资源 [com/example/config/JobConfig.class] 中定义的 bean 'step1'
- jquery - 空冰有什么问题,它不能工作
- reactjs - Enzyme & React 路由器:如何使用 useHistory 对组件进行浅层渲染
- hadoop - Windows 10 Hyper-v 上的 HDP 2.6.5
- angular - 在 Angular 中以父/子关系实现添加/更新/删除操作的最佳方法是什么?
- postgresql - 如何在不使用 Repo 软删除的情况下进行预加载
- python - 如何将值从一个数组存储到另一个数组?