xcode Swift - 弹出视图,日期选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29187286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:51:04  来源:igfitidea点击:

Swift - pop up view, date picker?

iosxcodeswift

提问by Max

I've googled for hours and have tried a handful of tutorials, but haven't been able to get this working:

我已经用谷歌搜索了几个小时并尝试了一些教程,但一直无法让这个工作:

I have a TableView, and I want to make it so pressing on a cell presents a popup that has a date picker.

我有一个 TableView,我想让它按下一个单元格会显示一个带有日期选择器的弹出窗口。

I have my custom viewcontroller with the date picker presenting (popping up from the bottom), but it takes up the entire screen. Thoughts? I found one mention of this exact issue while googling but the solution didn't work.

我有我的自定义视图控制器,带有日期选择器呈现(从底部弹出),但它占据了整个屏幕。想法?我在谷歌搜索时发现有人提到了这个确切的问题,但该解决方案不起作用。

回答by Syed Tariq

One possibility is to overlay a subview (object of UIView) (with a date picker and a done button) on top of your tableview. Then use .hidden feature of the subview to hide/show the view. The following is an example of the tableviewcontroller. When setting up the storyboard make sure that the subview has the layout constraints so the date picker is positioned properly. I used the "resolve auto layout issues" and it worked good. Unless you do special processing the subview will get positioned at the bottom of the rows. If you have a lot of rows the aubview will get clipped or hidden completely. So it is better to position the subview at the relative to the bottom of the page in your auto layout.

一种可能性是在 tableview 之上覆盖一个子视图(UIView 的对象)(带有日期选择器和完成按钮)。然后使用子视图的 .hidden 功能来隐藏/显示视图。下面是一个tableviewcontroller的例子。设置故事板时,请确保子视图具有布局约束,以便正确定位日期选择器。我使用了“解决自动布局问题”,效果很好。除非您进行特殊处理,否则子视图将位于行的底部。如果您有很多行,则 aubview 将被完全剪裁或隐藏。因此,最好将子视图定位在自动布局中相对于页面底部的位置。

Here is a simple example that worked well for me. In viewDidLoad the subview is hidden. When you click on any row it will show the subview and the date picker. When you press done it will hide the subview again.

这是一个简单的例子,对我来说效果很好。在 viewDidLoad 中隐藏子视图。当您单击任何行时,它将显示子视图和日期选择器。当您按下完成时,它将再次隐藏子视图。

import UIKit

class TableViewController: UITableViewController {

    @IBAction func doneButton(sender: UIButton) {
        // process the date using datePickerOutlet properties
        subView.hidden = true // hide the subview and its components
    }
    @IBOutlet weak var subView: UIView!
    @IBOutlet weak var datePickerOutlet: UIDatePicker!
    @IBAction func datePicker(sender: UIDatePicker) {
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        subView.hidden = true // hide the subview and its components
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("\(indexPath.row)")
        subView.hidden = false // show the subview and its components
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel!.text = "\(indexPath.row)"
        return cell
    }
}

回答by Victor Hazali

I think this should work:

我认为这应该有效:

override var preferredContentSize: CGSize {
    get{
        // Checks if it is currently presenting
        if presentingViewController != nil {
            return (datePicker.sizeThatFits(presentingViewController!.view.bounds.size))
        }
        return super.preferredContentSize
    }
    set{ super.preferredContentSize = newValue }
}

This code goes under the View controller for the popup. Basically what it does is to set the width of the popup to the minimum size required for the date picker.

此代码位于弹出窗口的视图控制器下。基本上它所做的是将弹出窗口的宽度设置为日期选择器所需的最小尺寸。

Got it from the iTunes U tutorial by Paul Hegarty

从 Paul Hegarty 的 iTunes U 教程中得到

回答by clearlight

Alternatively, put it in a UIAlertView(). At least it will be centered. It's going to be tough to combine a table and a picker on a small screen, like let's say a 4S.

或者,将它放在 UIAlertView() 中。至少它会居中。在小屏幕上结合一张桌子和一个选择器会很困难,比如 4S。