ios - 约束在 Present ViewController 中无法正常工作
问题描述
我有一个像 PopUp 这样的 ViewController,但 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: {})
编辑
我试过这个。
结果:(我确实改变了背景颜色)。
解决方案
您的问题是使用静态值设置顶部、底部、前导和尾随约束,当您在设备中运行时可能会中断,您需要设置
宽度,高度,centerX,centerY
* 使宽度和高度与屏幕成正比的良好做法,乘数或内容大小取决于 *
顶部标签
顶部,中心X
活动
顶部到标签,centerX
中间标签
中心X,Y
取消按钮
中心X,底部
推荐阅读
- sql - Oracle中的日期格式(0)转换
- ios - 2019 年在 App Store Connect 中哪里可以找到 2019 年测试版应用程序的应用程序 url 链接?
- python - numpy.fft 的有限精度低于某个值
- python-3.x - 将数据框导出到 csv 并且不覆盖 csv 中包含公式的单元格
- java - 用于生成多边形的 Imagemagick 转换命令在命令行中工作,但在 CentOS 中不起作用
- java - 同步发送 JMS 消息
- apache-spark - 如何理解 Spark 中的任务并行性是否有效?
- javascript - 设置和访问 Vue.prototype 值
- html - 关于减小屏幕尺寸 - 侧边导航栏与主要内容重叠,并在浏览器窗口屏幕的右侧留出空白
- xsd - 基于枚举值的可选元素