添加按钮到UIPickerView - Swift 1.2

时间:2015-02-25 13:45:11

标签: ios swift uibutton uipickerview

我想在UIButtons(在其上面)添加两个UIPickerView。请查看此图片中的CancelDone按钮:

enter image description here

我该怎么办?

3 个答案:

答案 0 :(得分:26)

我已经解决了这个问题,在Swift中回答:

var pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
pickerView.backgroundColor = .whiteColor()
pickerView.showsSelectionIndicator = true

var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()


let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: "canclePicker")

toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true

textField.inputView = pickerView
textField.inputAccessoryView = toolBar

答案 1 :(得分:2)

见我的。我有Collection View和UIPicker来选择照片文件夹。

enter image description here

//MARK:- SHOW PHOTO FOLDER BUTTON METHOD
@IBAction func photoFolderListButtonClicked(sender: UIButton) {
    self.navigationItem.setHidesBackButton(true, animated: true)

    //Making PickerView of Competitors
    let DEVICE_SCREEN_WIDTH =    UIScreen.mainScreen().bounds.size.width
    photoFolderListPicker.frame = CGRectMake(0.0, 44.0, DEVICE_SCREEN_WIDTH, 260)
    photoFolderListPicker.dataSource = self
    photoFolderListPicker.delegate = self
    photoFolderListPicker.showsSelectionIndicator = true;
    photoFolderListPicker.backgroundColor = UIColor.whiteColor()

    let pickerDateToolbar = UIToolbar(frame: CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, 44))
    pickerDateToolbar.barStyle = UIBarStyle.Black
    pickerDateToolbar.barTintColor = UIColor.blackColor()
    pickerDateToolbar.translucent = true

    //Cancel button on photoFolderListPicker
    let barItems = NSMutableArray()
    let labelCancel = UILabel()
    labelCancel.text = " Cancel"

    let titleCancel = UIBarButtonItem(title: labelCancel.text, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("cancelPickerSelectionButtonClicked:"))
    titleCancel.tintColor = UIColor.whiteColor()
    barItems.addObject(titleCancel)

    var flexSpace: UIBarButtonItem
    flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
    barItems.addObject(flexSpace)

    //Done Button on photoFolderListPicker
    let doneBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("photoFolderSelectionDone:"))
    barItems.addObject(doneBtn)
    doneBtn.tintColor = UIColor.whiteColor()

    pickerDateToolbar.setItems(barItems as [AnyObject] as? [UIBarButtonItem], animated: true)
    actionView.addSubview(pickerDateToolbar)
    actionView.addSubview(photoFolderListPicker)

    if (window != nil) {
        window!.addSubview(actionView)
    }
    else {
        self.view.addSubview(actionView)
    }
    UIView.animateWithDuration(0.2, animations: {
        self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height - 250.0, UIScreen.mainScreen().bounds.size.width, 250)
    })
}
//MARK:- PICKERVIEW DELEGATE METHODS
func pickerView(_pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return model.documentFolders.count
}
func numberOfComponentsInPickerView(_pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(_pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return model.documentFolders[row].value
}
//MARK:- PICKERVIEW BUTTON METHODS
func cancelPickerSelectionButtonClicked(sender: UIBarButtonItem) {
    self.navigationItem.setHidesBackButton(false, animated: true)

    UIView.animateWithDuration(0.2, animations: {
        self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width, 210.0)
        }, completion: { _ in
            for obj: AnyObject in self.actionView.subviews {
                if let view = obj as? UIView
                {
                    view.removeFromSuperview()
                }
            }
    })
    photoFolderListButton.setTitle(" "+"Select Folder", forState: UIControlState.Normal)
}
func photoFolderSelectionDone(sender: UIBarButtonItem) {
    uploadedPhotoFolderName?.isEmpty
    loadedFromPageSection = "fromPhotoFolderSelectionDone"
    self.disableScoutDetailUserInterfaceElementsDutingApiCall()

    //setting selectedPhotoFolderItem and selectedPhotoFolderItemId
    let myRow = photoFolderListPicker.selectedRowInComponent(0)
    selectedPhotoFolderItem =  model.documentFolders[myRow].value!
    selectedPhotoFolderItemId = Int(model.documentFolders[myRow].key!)

    UIView.animateWithDuration(0.2, animations: {
        self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width, 260.0)
        }, completion: { _ in
            for obj: AnyObject in self.actionView.subviews {
                if let view = obj as? UIView
                {
                    view.removeFromSuperview()
                }
            }
    })
    photoFolderListButton.setTitle(" "+(selectedPhotoFolderItem as? String)!, forState: UIControlState.Normal)
    self.view.makeToastActivityWithMessage(message: "Fetching")

    //Delegate is added to class after popViewControllerAnimated(true) called
    model.delegate = self

    //API CALL - Fetching Photos
    AppController().fetchPropertyPhotosGallery(hbId, propertyId: scoutPropertyId, fileTypeId: selectedPhotoFolderItemId)

}

答案 2 :(得分:1)

以下是我在ObjC中如何使用按钮向文本字段添加选择器弹出窗口。应该足够简单,可以转化为快速。基本上,您创建选择器并将其分配给文本inputView。然后创建一个工具栏,您可以将其分配给文本inputAccessoryView,该文本在弹出时位于inputView之上。

self.manufacturerPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
self.manufacturerPicker.delegate = self;
self.manufacturerPicker.dataSource = self;
[self.manufacturerPicker setShowsSelectionIndicator:YES];
self.filterByManufacturerTextField.inputView = self.manufacturerPicker;

UIToolbar *manufacturerPickerToolbar=[[UIToolbar alloc] init];
manufacturerPickerToolbar.barStyle = UIBarStyleBlack;
manufacturerPickerToolbar.translucent = YES;
manufacturerPickerToolbar.tintColor = nil;
[manufacturerPickerToolbar sizeToFit];

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                style:UIBarButtonItemStyleBordered target:self
                                                              action:@selector(manufacturerPickerDoneClicked:)];
UIBarButtonItem *spacerButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                style:UIBarButtonItemStyleBordered target:self
                                                               action:@selector(manufacturerPickerCancelClicked:)];

[manufacturerPickerToolbar setItems:@[cancelButton,spacerButton,doneButton]];
self.filterByManufacturerTextField.inputAccessoryView = manufacturerPickerToolbar;

在没有UITextField作为起点时进行了更新:

如果你想从一个单元格中启动一个选择器,就像它是一个文本字段选择器一样,我相信你必须让你的UITableViewCell实现成为第一个响应者的过程。

为此,我认为该过程是在您的单元格中实现UIResponder协议。

1)覆盖canBecomeFirstResponder并返回YES。

2)覆盖inputView属性getter。在此代码中返回您的UIPickerView。

§3)覆盖inputAccessoryView属性getter。在此代码中返回您的UIToolbar。

当您选择单元格时,现在在您的单元格中,通过调用[self becomeFirstResponder];使单元格成为第一个响应者。

然后,我认为应该正确地启动选择器和工具栏。可能还有更多。

取消或完成后,请使用[self resignFirstResponder];

将其删除

之前没有这样做,但我认为这是一般过程。

选项2(作弊):

有时作弊更容易。在单元格的后面添加一个隐藏的UITextField,或者添加一个宽度为0的UITextField。根据原始建议设置其inputView和inputAccessoryView。

然后当您选择单元格时,只需使用[self.<your text field property> becomeFirstResponder];

将UITextField作为第一个响应者

这应该通过后门给你相同的结果。