【问题标题】:Is it possible to change the Y-position of action sheet styled UIAlertController?是否可以更改样式为 UIAlertController 的操作表的 Y 位置?
【发布时间】:2021-02-22 06:06:55
【问题描述】:

当前,我正在通过以下代码显示操作表样式 UIAlertController

@IBAction func attachmentButtonClicked(_ sender: Any) {
    let chooseImageImage = UIImage(systemName: "photo")
    let takePhotoImage = UIImage(systemName: "camera")
    let drawingImage = UIImage(systemName: "paintbrush.pointed")
    let recordingImage = UIImage(systemName: "mic")

    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    let chooseImageAction = UIAlertAction(title: "choose_image".localized, style: .default) {
        UIAlertAction in
        // Write your code here
    }
    chooseImageAction.setValue(chooseImageImage, forKey: "image")

    let takePhotoAction = UIAlertAction(title: "take_photo".localized, style: .default) {
        UIAlertAction in
        // Write your code here
    }
    takePhotoAction.setValue(takePhotoImage, forKey: "image")

    let drawingAction = UIAlertAction(title: "drawing".localized, style: .default) {
        UIAlertAction in
        // Write your code here
    }
    drawingAction.setValue(drawingImage, forKey: "image")

    let recordingAction = UIAlertAction(title: "recording".localized, style: .default) {
        UIAlertAction in
        // Write your code here
    }
    recordingAction.setValue(recordingImage, forKey: "image")

    let cancelAction = UIAlertAction(title: "Cancel".systemLocalized, style: .cancel) {
        UIAlertAction in
        // It will dismiss action sheet
    }

    alert.addAction(chooseImageAction)
    alert.addAction(takePhotoAction)
    alert.addAction(drawingAction)
    alert.addAction(recordingAction)
    alert.addAction(cancelAction)

    // https://stackoverflow.com/questions/55653187/swift-default-alertviewcontroller-breaking-constraints
    self.present(alert, animated: true, completion: nil)
}

结果如下


我想知道,有没有办法调整UIAlertController 的 Y 位置,这样就不会阻挡底部工具栏的可见性?我希望达到如下截图所示的效果。

【问题讨论】:

  • 您不能自定义 UIAlertController。只需编写您自己呈现的视图控制器并将其放置在您喜欢的位置。

标签: ios swift


【解决方案1】:

你需要找到子视图,然后改变底部约束的常量。

\\-----OtherCode-----

if let bottomConstraint = alert.view.subviews[0].subviews.last?.constraints.first(where: { ($0.firstAttribute == .bottom) }) {
    bottomConstraint.constant = bottomConstraint.constant - 50
}
self.present(alert, animated: true, completion: nil)

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2011-05-04
    • 2016-06-10
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多