首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Swift 5.1中添加图片和文本后添加UISwitch到UIAlertAction

如何在Swift 5.1中添加图片和文本后添加UISwitch到UIAlertAction
EN

Stack Overflow用户
提问于 2020-07-08 12:51:30
回答 1查看 171关注 0票数 0

在添加图像和文本后,我想在UIAlertAction中添加UISwitch。我尝试了不同的方法,但它不能正常工作,有人可以帮助我如何做到这一点,并感谢您的意见和反馈。请参阅附件中的代码和截图了解更多细节。我已经展示了我需要通过红色箭头添加UISwitch的地方。

源代码

代码语言:javascript
运行
复制
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
    }
    
    
} 

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-24 11:42:17

最终,我能够做到这一点。很抱歉回复得太晚了。我们有一个名为contentViewController的键,它用于在UIAlertAction中自定义UI元素。所以,请检查我的最新代码,并尝试它。我有附加的最终输出截图。谢谢..。

代码:

代码语言:javascript
运行
复制
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)
    }
    
    
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62787775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档