3

I have created an alertcontroller with action sheet and two buttons. As the actionsheet will be having rounded edges, on the screen, on four corners, white color is appearing. I tried changing the background color of the alertcontroller, but that is making the action sheet to rectangular with sharp border instead of rounded borders. I tried setting the view background color to clear color Tried setting the border radius too. Here is my action sheet. I want those white edges invisible.

enter image description here

Also, how to change the background color of Cancel.

let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel) { _ in
    // this is where I am creating the cancelAction
}

Edit - 1 : Adding code for alertcontroller

func showActionSheet(_ changeAction: UIAlertAction) {
    let alertController = UIAlertController(title: "", message: "Here is my alert text".localize(), preferredStyle: .actionSheet)

    alertController.view.tintColor = StyleKit.goldenColor
    let attributedString = NSAttributedString(string: alertController.message!, attributes: [
        NSForegroundColorAttributeName : StyleKit.whiteColor
        ])
    alertController.setValue(attributedString, forKey: "attributedMessage")
    if let subview = alertController.view.subviews.first, let alertContentView = subview.subviews.first {
        for innerView in alertContentView.subviews {
            innerView.backgroundColor = StyleKit.popoverDefaultBackgroundColor

        }

    }
    let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel) { _ in
        self.doneButton.isEnabled = true
    }

    alertController.addAction(changeAction)
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true)
}
krishna
  • 33
  • 4

1 Answers1

5

here is the solution of this white border of UIAlertAction actionSheet

let actionSheet = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)

if let subview = actionSheet.view.subviews.first, let actionSheet = subview.subviews.first {
    for innerView in actionSheet.subviews {
        innerView.backgroundColor = .purple
        innerView.layer.cornerRadius = 15.0
        innerView.clipsToBounds = true
    }
}

actionSheet.addAction(UIAlertAction.init(title: "Take Photo", style: UIAlertActionStyle.default, handler: { (action) in

}))
actionSheet.addAction(UIAlertAction.init(title: "Choose Photo", style: UIAlertActionStyle.default, handler: { (action) in

}))
actionSheet.addAction(UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (action) in
}))

actionSheet.view.tintColor = .orange

self.present(actionSheet, animated: true, completion: nil)

Update with hex color

Github : https://github.com/BhaveshDhaduk/AlertControllerSwift

enter image description here

Bhavesh Dhaduk
  • 1,888
  • 15
  • 30
  • Both will be changing the background color (which you gave as purple). The fix is not working as both last and first are changing the background color of "take photo and choose photo". Not the boarder line. This is the color i have been using. UIColor(hex: "0D324E") – krishna Jun 23 '17 at 12:25
  • It's work for me, first is the only part : innerView.layer.cornerRadius = 15.0 just change the radios of action sheet, check this meanwhile i will check with hax color which you provide. – Bhavesh Dhaduk Jun 23 '17 at 12:44
  • I dont know why this fix is not working in my code. But your code is perfect. I tried it with other sample. Do you have any idea how to change the Cancel button background color? – krishna Jul 03 '17 at 05:27