问题
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.
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)
}
回答1:
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
来源:https://stackoverflow.com/questions/44719277/alertcontroller-with-white-borders