【问题标题】:Why button appears only on the second attempt?为什么按钮仅在第二次尝试时出现?
【发布时间】:2019-08-15 09:26:03
【问题描述】:

我正在尝试添加一个功能,该功能将在我的自定义警报视图中显示关闭按钮。 当我第一次调用它时,只出现一个不包含按钮的空白区域(没有颜色,没有文本,也没有按下)。当您再次调用时 - 一切正常。

它不依赖于参数animated,在两种情况下它的工作原理都是一样的。

我的dialogView 是我的自定义UIView。我不使用UIAlertController

可能是什么问题?

initial viewafter the first attemptsecond 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?()
            }
        }
    }

【问题讨论】:

  • @RobertDresler 对不起,也许我写错了。我添加到描述中 - 我不使用 UIAlertController
  • 您已经为ifelse 两种情况添加了self.dialogView.addSubview(button)。所以最好在if-else之前或之后添加按钮。
  • @AmirKhan 但是在第一种情况if,它不是默认使用,而是仅在完成块中使用。
  • 好的,明白了。最好根据场景添加该按钮和show/hide。或在主线程中添加按钮 - DispatchQueue.main.async { self.dialogView.addSubview(button) }
  • @AmirKhan 现在我尝试添加这段代码并在if-else 块之前和之后执行它,结果是一样的。

标签: ios swift uiview uibutton addsubview


【解决方案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.

如果您仍有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2019-08-24
    • 2017-10-24
    相关资源
    最近更新 更多