简体   繁体   English

如何在UIPickerView(swift 2.0)中添加两个按钮来添加UIToolbar?

[英]How to Add UIToolbar with two button in UIPickerView(swift 2.0)?

I would like to add two UIButtons in the UIPickerView (on bottom of it). 我想在UIPickerView中添加两个UIButton(在它的底部)。 Please take a look at the Cancel and Done buttons in this image: 请查看此图片中的取消和完成按钮:

在此输入图像描述

Here My code Upload:- 这里我的代码上传: -

class DatePicker{

var containerView = UIView()
var datePicker = UIView()
var datePickerView = UIDatePicker()
var toolBar = UIToolbar()
internal class var shared: DatePicker {
    struct Static {
        static let instance: DatePicker = DatePicker()
    }
    return Static.instance
}
internal func showProgressView(view: UIView) {
    containerView.frame = view.frame
    containerView.center = view.center
    containerView.backgroundColor = UIColor(hex: 0xffffff, alpha: 0.3)
    datePickerView.datePickerMode = .Date
    datePicker.frame = CGRectMake(0, 0, 250, 250)
    datePicker.center = view.center
    datePicker.backgroundColor = UIColor(hex: 0x444444, alpha: 0.7)
    datePicker.clipsToBounds = true
    datePicker.layer.cornerRadius = 10
    datePickerView.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
    datePickerView.frame = CGRectMake(0, 0, 250, 250)
    datePicker.addSubview(datePickerView)
    containerView.addSubview(datePicker)
    view.addSubview(containerView)
}
internal func hideProgressView() {
    containerView.removeFromSuperview()
}

} }

How can i get UIToolbar And Two Button? 我怎样才能获得UIToolbar和两个按钮?

I would recommend making a .xib with the toolbar and picker view in it. 我建议使用工具栏和选择器视图制作.xib。 Put two bar buttons on the bar and put a bar button spacer in between them. 在条上放置两个条形按钮,并在它们之间放置一个条形按钮垫片。

Link on how to use a xib 关于如何使用xib的链接

You can make the animation of it appearing look great if you start its frame off screen and use this function to move it on screen. 如果你在屏幕上启动它的框架并使用此功能在屏幕上移动它,你可以让它的动画看起来很棒。

UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
        if viewToMove != nil {
            viewToMove!.frame.origin.y/*orX*/ = onScreenPosition
        } else {
            "sidePanel == nil"
        }
}, completion: completion)

usingSpringWithDamping can be set to 1 if you don't want any bounce in animation. 如果您不想在动画中进行任何反弹,则可以将usingSpringWithDamping设置为1。 If this is for an iPhone, i would recommend making the initial frame equal to something like 如果这是为iPhone,我会建议使初始帧等于

CGRectMake(0, UIScreen.mainScreen().bounds.height, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height/2)

and then the view to move line would become 然后移动线的视图将变为

viewToMove!.frame.origin.y = UIScreen.mainScreen().bounds.height/2

using the previous function to bring it on screen and 使用上一个功能将其带到屏幕上

viewToMove!.frame.origin.y = UIScreen.mainScreen().bounds.height

to move it off screen. 把它从屏幕上移开 You can access the buttons of the xib in a few different ways, but I recommend using a delegate. 您可以通过几种不同的方式访问xib的按钮,但我建议使用委托。

Here is a guide to them. 这是他们的指南。

If you are not familiar with them, I would become familiar, because they are very useful! 如果你不熟悉它们,我会熟悉,因为它们非常有用! If you have any questions don't hesitate to ask! 如果您有任何疑问,请不要犹豫!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM