【发布时间】:2020-07-08 04:51:30
【问题描述】:
我想在添加图像和文本后将UISwitch 添加到UIAlertAction。我尝试了不同的方法,但它不能正常工作有人可以帮助我如何做到这一点并感谢您的 cmets 和反馈。请参阅附加的代码和屏幕截图以获取更多详细信息。我已经通过红色箭头显示了我需要添加UISwitch 的地方。
源代码
import UIKit
class ViewController: UIViewController {
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
//
}
override func viewWillLayoutSubviews() {
let width = self.view.frame.width
let navigationBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: width, height: 44))
self.view.addSubview(navigationBar);
let navigationItem = UINavigationItem(title: "Navigation bar")
let rightButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(openMenuPopHandler(_:)))
navigationItem.rightBarButtonItem = rightButton
navigationBar.setItems([navigationItem], animated: false)
}
@IBAction func openMenuPopHandler(_ sender: UIBarButtonItem) {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
//TODO Action 1 impl
}
let icon1 = UIImage(systemName: "music.note", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .bold))?.withTintColor(.black)
action1.setValue(icon1?.withRenderingMode(.alwaysOriginal), forKey: "image")
action1.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
action1.setValue(UIColor.black, forKey: "titleTextColor")
let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
//TODO Action 2 impl
}
let icon2 = UIImage(systemName: "music.note", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .bold))?.withTintColor(.black)
action2.setValue(icon2?.withRenderingMode(.alwaysOriginal), forKey: "image")
action2.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
action2.setValue(UIColor.black, forKey: "titleTextColor")
alertController.addAction(action1)
alertController.addAction(action2)
alertController.modalPresentationStyle = .popover
let presentationController = alertController.popoverPresentationController
presentationController?.barButtonItem = sender
present(alertController, animated: true, completion: nil)
let subview = (alertController.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
subview.layer.cornerRadius = 0
subview.backgroundColor = .white
}
}
【问题讨论】:
标签: swift5 uiswitch uialertaction