Reputation: 13
I want to create a popover menu with UIAlertController which includes customize action items, user can define style like font-size , background-color, alignment and Icon Position (Leading / Trailing), how to change icon position, make the icon behind the title ?
I use the following extension to set color and alignment, but don't know how to set font-size and icon position, please help me find a solution.
extension UIAlertAction {
var titleTextColor: UIColor? {
get { return self.value(forKey: "titleTextColor") as? UIColor }
set { self.setValue(newValue, forKey: "titleTextColor") }
}
var titleAlignment: Int? {
get { return self.value(forKey: "titleTextAlignment") as? Int }
set { self.setValue(newValue, forKey: "titleTextAlignment") }
}
}
Upvotes: 1
Views: 43
Reputation: 271810
UIAlertAction
does not supporting changing the image's position. I would suggest writing your own alert controller instead of using the built-in one.
That said, it is possible to move the images of all alert actions to the right if you override the layout direction, since the images always appear on the leading side.
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let action1 = UIAlertAction(title: "Action 1", style: .default) { _ in }
action1.setValue(UIImage(...), forKey: "image")
alert.addAction(action1)
// assuming this is in a UIViewController subclass
present(alert, animated: true)
if #available(iOS 17.0, *) {
alert.traitOverrides.layoutDirection = .rightToLeft
} else {
setOverrideTraitCollection(.init(layoutDirection: .rightToLeft), forChild: alert)
}
Upvotes: 0