【问题标题】:How to add UISwitch to UIAlertAction after adding image and text in Swift 5.1在 Swift 5.1 中添加图像和文本后如何将 UISwitch 添加到 UIAlertAction
【发布时间】: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


    【解决方案1】:

    终于,我做到了。很抱歉迟到了回复。 我们有一个键,它被称为contentViewController,用于自定义UIAlertAction 内部的UI 元素。所以请检查我的最新代码并尝试一下。我附上了最终输出的屏幕截图。谢谢...

    代码:

    import UIKit
    
    class ViewController: UIViewController {
        
        class UISwitchController: UIViewController {
            var uiSwitch: UISwitch!
            
            init(){
                self.uiSwitch = UISwitch()
                super.init(nibName: nil, bundle: nil)
            }
            
            required init?(coder: NSCoder) {
                fatalError("init(coder:) has not been implemented")
            }
            
            override func viewDidLoad() {
                super.viewDidLoad()
                uiSwitch.addTarget(self, action: #selector(siwtchOperation(_:)), for: .touchUpInside)
                uiSwitch.translatesAutoresizingMaskIntoConstraints = false
                self.view.addSubview(uiSwitch)
            }
            
            @IBAction func siwtchOperation(_ sender: UISwitch){
                //TODO operation impl
            }
            
            func setPosition(){
                NSLayoutConstraint.activate([
                    uiSwitch.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -20),
                    uiSwitch.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
                    self.view.rightAnchor.constraint(equalTo: self.view.superview!.rightAnchor, constant: 0),
                    self.view.centerYAnchor.constraint(equalTo: self.view.superview!.centerYAnchor)
                ])
            }
            
            func setUiSwitchVal(isOn: Bool){
                uiSwitch.isOn = isOn
            }
            
        }
        
        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 uiSwitchController1 = UISwitchController()
            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")
            action1.setValue(uiSwitchController1, forKey: "contentViewController")
            action1.isEnabled = false
            
            let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
                //TODO Action 2 impl
            }
            let uiSwitchController2 = UISwitchController()
            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")
            action2.setValue(uiSwitchController2, forKey: "contentViewController")
            action2.isEnabled = false
            
            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
            
            uiSwitchController1.setPosition()
            uiSwitchController1.setUiSwitchVal(isOn: true)
            uiSwitchController2.setPosition()
            uiSwitchController2.setUiSwitchVal(isOn: false)
        }
        
        
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      相关资源
      最近更新 更多