首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在UIActionSheet中创建UIPickerView

在UIActionSheet中创建UIPickerView可以通过以下步骤实现:

  1. 首先,在UIActionSheet的代理方法中添加UIPickerView:
代码语言:swift
复制
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex == 0 {
        let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 320, height: 216))
        pickerView.delegate = self
        pickerView.dataSource = self
        actionSheet.addSubview(pickerView)
    }
}
  1. 然后,实现UIPickerView的代理方法:
代码语言:swift
复制
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return options.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return options[row]
}
  1. 最后,在UIActionSheet的代理方法中添加UIPickerView的布局约束:
代码语言:swift
复制
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex == 0 {
        let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 320, height: 216))
        pickerView.delegate = self
        pickerView.dataSource = self
        actionSheet.addSubview(pickerView)

        // 添加布局约束
        pickerView.translatesAutoresizingMaskIntoConstraints = false
        actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .centerX, relatedBy: .equal, toItem: actionSheet, attribute: .centerX, multiplier: 1, constant: 0))
        actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .bottom, relatedBy: .equal, toItem: actionSheet, attribute: .bottom, multiplier: 1, constant: 0))
        actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 320))
        actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 216))
    }
}

这样就可以在UIActionSheet中创建UIPickerView了。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

iOS8统一的系统提示控件——UIAlertController

相信在iOS开发中,大家对UIAlertView和UIActionSheet一定不陌生,这两个控件在UI设计中发挥了很大的作用。然而如果你用过,你会发现这两个控件的设计思路有些繁琐,通过创建设置代理来进行界面的交互,将代码逻辑分割了,并且很容易形成冗余代码。在iOS8之后,系统吸引了UIAlertController这个类,整理了UIAlertView和UIActionSheet这两个控件,在iOS中,如果你扔使用UIAlertView和UIActionSheet,系统只是会提示你使用新的方法,iOS9中,这两个类被完全弃用,但这并不说明旧的代码将不能使用,旧的代码依然可以工作很好,但是会存在隐患,UIAlertController,不仅系统推荐,使用更加方便,结构也更加合理,作为开发者,使用新的警示控件,我们何乐而不为呢。这里有旧的代码的使用方法:

01

iOS流布局UICollectionView系列六——将布局从平面应用到空间

前面,我们将布局由线性的瀑布流布局扩展到了圆环布局,这使我们使用UICollectionView的布局思路大大迈进了一步,这次,我们玩的更加炫一些,想办法将布局应用的空间,你是否还记得,在管理布局的item的具体属性的类UICollectionViewLayoutAttributrs类中,有transform3D这个属性,通过这个属性的设置,我们真的可以在空间的坐标系中进行布局设计。iOS系统的控件中,也并非没有这样的先例,UIPickerView就是很好的一个实例,这篇博客,我们就通过使用UICollectionView实现一个类似系统的UIPickerView的布局视图,来体会UICollectionView在3D控件布局的魅力。系统的pickerView效果如下:

02
领券