Изменить цвкет, фон, текстовые поля в AlertController (Стилизация UIAlertController) - Swift 5
func addDialog() {
let dialog = UIAlertController(title: "", message: "", preferredStyle: .alert)
// Title
let attributedStringForTitle =
NSAttributedString(
string: "TITLE",
attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16), NSAttributedString.Key.
foregroundColor:
UIColor.yellow])
dialog.setValue(attributedStringForTitle, forKey: "
attributedTitle")
// Message
let attributedStringForMessage =
NSAttributedString(string: "DESCR", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.
foregroundColor:
UIColor.white])
dialog.setValue(attributedStringForMessage, forKey: "
attributedMessage")
// View
dialog.view.subviews.first?.subviews.first?.subviews.first?.
backgroundColor = .gray
// Button text color
dialog.view.tintColor = .white
// First textField
dialog.addTextField { (
serv)
in
serv.backgroundColor = .black
// Changing text color
serv.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes:[NSAttributedString.Key.
foregroundColor:
UIColor.darkGray])
}
// Second textField
dialog.addTextField { (
price)
in
price.backgroundColor = .black
// Changing text color
price.attributedPlaceholder = NSAttributedString(string:"Enter price", attributes:[NSAttributedString.Key.
foregroundColor:
UIColor.darkGray])
}
dialog.textFields?[0].superview?.backgroundColor = .black
let view1 = dialog.textFields?[0].superview?.superview
view1?.subviews.first?.
alpha = 0
view1?.
backgroundColor = .black
dialog.textFields?[1].superview?.backgroundColor = .black
let view2 = dialog.textFields?[1].superview?.superview
view2?.subviews.first?.
alpha = 0
view2?.
backgroundColor = .black
let buttonAdd =
UIAlertAction(
title: "Add",
style: .default) { (_)
in
// Add button action
}
let buttonCancel =
UIAlertAction(
title: "Cancel",
style: .cancel) { (_)
in }
dialog.addAction(buttonAdd)
dialog.addAction(buttonCancel)
present(dialog,
animated: true)
}
Возврат к списку