UIButton 位置改变|用户界面窗口 |重播套件

UIButton Position Changing| UIWindow | ReplayKit

我正在使用 Replaykit 来录制屏幕...并使用以下代码从录制中排除录像机 UIButton...效果很好。录制视频后,我会发出警报并提供编辑或删除选项。当我 select 编辑和取消时,我会得到我的主视图作为 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))