0

Current, I am showing action sheet style UIAlertController via the following code

@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)
}

The outcome is as following

enter image description here


I was wondering, is there a way to adjust the Y-position of UIAlertController, so that it will not block the bottom toolbar visibility? I wish the achieve the effect as shown in the following screenshot.

enter image description here

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • 1
    You can’t customize a UIAlertController. Just write your own presented view controller and place it where you like. – matt Feb 22 '21 at 06:21

1 Answers1

0

You need to find the subview and then change the constant of the bottom constraint.

\\-----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)
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52