如何将系统项添加到按钮和操作表

时间:2016-10-07 06:11:54

标签: ios swift xcode

我想在Add,Edit的按钮中添加 UIViewcontroller 等系统项。我能够在Attribute Inspector下找到BarButton项目的系统项目,但不能找到按钮和ActionSheet。有没有办法实现这个功能。

1 个答案:

答案 0 :(得分:1)

您可能需要创建自己的方法并循环显示临时 BarButtonItem 的子视图。

此功能可以帮助您(用 Swift 3.0 编写)。

func imageFromBarButtonItem(type: UIBarButtonSystemItem) -> UIImage {
        let barButtonItem = UIBarButtonItem(barButtonSystemItem: type, target: nil, action: nil)            

        // You have to render it somewhere first
        // This is actually not affecting you design
        // This is rendering only so the system has it in memory
        UIToolbar().setItems([barButtonItem], animated: false)

        // Now you can loop and get the image
        let itemView = barButtonItem.value(forKey:"view") as! UIView
        for view in itemView.subviews {
            if view.isKind(of: UIButton.self){
                let button = view as! UIButton
                return button.imageView!.image!
            }
        }            
        return UIImage()
    }

用法:

myView.image = self.imageFromBarButtonItem(type: UIBarButtonSystemItem.add)