我有一个像 PopUp 这样的 ViewController,但 View 没有停留在我想要的位置。
我希望此 View 保持在屏幕中央。下面的屏幕显示了 Storyboard中的 View 以及设置的约束和模拟器上的结果。
AlertViewController.swift
class AlertViewController : UIViewController {
@IBOutlet weak var frameView: UIView!
@IBOutlet weak var btCancel: UIButton!
@IBOutlet weak var lblInfo: UILabel!
var delegate : AlertViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.frameView.layer.cornerRadius = 10
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
@IBAction func cancel(_ sender: Any) {
delegate?.cancelBt()
self.dismiss(animated: true, completion: nil)
}
}
MainViewController.swift(显示)
let alertController = self.storyboard?.instantiateViewController(withIdentifier: "AlertVC") as! AlertViewController
alertController.providesPresentationContextTransitionStyle = true
alertController.definesPresentationContext = true
alertController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
alertController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
alertController.delegate = self
self.present(alertController, animated: true, completion: {})
编辑
我尝试过这个。
结果:(我确实改变了背景颜色)。
最佳答案
您的问题是使用静态值设置顶部、底部、前导和尾随约束,这些约束在您在设备中运行时可能会中断,您需要设置
width,height,centerX,centerY
* 使宽度和高度与屏幕成正比的好习惯,并且乘数或内容大小取决于 *
顶部标签
top , centerX
事件
top to label , centerX
中间标签
centerX,Y
取消按钮
centerX , bottom
关于ios - 约束在当前 ViewController 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52503948/