ios - 约束在当前 ViewController 中无法正常工作

标签 ios swift xcode layout constraints

我有一个像 PopUp 这样的 ViewController,但 View 没有停留在我想要的位置。

我希望此 View 保持在屏幕中央。下面的屏幕显示了 Storyboard中的 View 以及设置的约束和模拟器上的结果。

enter image description here

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: {})

编辑

我尝试过这个。

enter image description here

结果:(我确实改变了背景颜色)。

enter image description here

最佳答案

您的问题是使用静态值设置顶部、底部、前导和尾随约束,这些约束在您在设备中运行时可能会中断,您需要设置

width,height,centerX,centerY

* 使宽度和高度与屏幕成正比的好习惯,并且乘数或内容大小取决于 *

顶部标签

top , centerX

事件

top to label , centerX

中间标签

centerX,Y

取消按钮

centerX , bottom

关于ios - 约束在当前 ViewController 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52503948/

相关文章:

ios - xcrun 无法复制到/var/文件夹

ios - 尝试在文本字段的应用程序电子邮件中添加

ios - 在 Swift 中将图像发送到 ViewController 的多个实例

ios - swift 错误 : Can't assign value of type UIColor to type CGColor

ios - Xcode 6 这么多模拟器困惑

iphone - Foursquare无用户搜索iOS

ios - 无法展开 'Optional.None'

swift - UIScrollView 中的 UIStackView 中的点击检测 UIImageView

ios - 已打开应用程序中的 URL Scheme

iphone - 如何使用 JSON-Framework 解析 iPhone SDK (XCode) 中的 JSON 对象