我正在与Replaykit一起使用来录制屏幕..并使用以下代码从录制中排除录像机UIButton ...效果很好。录制视频后,我将显示警报并提供编辑或删除的选项。当选择编辑并取消时,我将以rootviewcontroller的身份进入主视图。

问题:取消两次后...按钮位置更改并设置为视图的左上角(buttonWindow)

码:

 func addButtons( button1: UIButton) {
    self.buttonWindow = UIWindow(frame: self.view.frame)
    self.buttonWindow.rootViewController = HiddenStatusBarViewController()
    self.buttonWindow.rootViewController?.view.addSubview(button1)
    self.buttonWindow.rootViewController?.view.updateConstraintsIfNeeded()
    self.buttonWindow.makeKeyAndVisible()

}


override func viewDidLayoutSubviews() {
    print(3)
    self.videoBtn.frame = (CGRect(x: 155, y: 575, width: 65, height: 65))

}
    func startRecording() {

    DispatchQueue.main.async {
        // Update UI
        self.addButtons(button1: self.videoBtn)


    }


    self.videoBtn.setImage(UIImage(named:"video capture"), for: .normal)
    self.videoBtn.tintColor = .red

    guard recorder.isAvailable else {
        print("Recording is not available at this time.")
        self.videoBtn.tintColor = .white
        return
    }

    recorder.isMicrophoneEnabled = true


    recorder.startRecording{ [unowned self] (error) in

        guard error == nil else {
            print("There was an error starting the recording.")
            self.videoBtn.tintColor = .white
            return
        }

        print("Started Recording Successfully")

        self.isRecording = true

    }

}

func stopRecording() {

    self.videoBtn.tintColor = .white
    recorder.stopRecording { [unowned self] (preview, error) in
        print("Stopped recording")


        guard preview != nil else {
            print("Preview controller is not available.")
            return
        }

        let alert = UIAlertController(title: "Recording Finished", message: "Would you like to edit or delete your recording?", preferredStyle: .alert)

        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action: UIAlertAction) in
            self.recorder.discardRecording(handler: { () -> Void in
                print("Recording suffessfully deleted.")

            })
        })

        let editAction = UIAlertAction(title: "Edit", style: .default, handler: { (action: UIAlertAction) -> Void in
            preview?.previewControllerDelegate = self.buttonWindow.rootViewController as? RPPreviewViewControllerDelegate
            self.buttonWindow.rootViewController?.present(preview!, animated: true, completion: nil)
        })

        alert.addAction(editAction)
        alert.addAction(deleteAction)
        self.buttonWindow.rootViewController?.present(alert, animated: true, completion: nil)

        self.isRecording = false

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard.instantiateViewController(withIdentifier: "ViewController")


        let app = UIApplication.shared
        app.keyWindow?.rootViewController = rootController

    }
    DispatchQueue.main.async {
        // Update UI
       }

}

func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
    dismiss(animated: true)
}


谢谢 :)

最佳答案

我通过删除自动布局解决了这个问题!这样就可以了。谢谢!

如果您想使用自动布局?那么下面的代码就可以了!

self.buttonWindow.addConstraint(NSLayoutConstraint(item: button1, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.buttonWindow, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))

self.buttonWindow.addConstraint(NSLayoutConstraint(item: button1, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.buttonWindow, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -50))

09-06 11:38