子视图的位置不是我的预期

时间:2016-04-23 07:32:12

标签: ios swift uiview uidatepicker

我添加了一个日期选择器作为子视图,我期望的是当我按下"更改日期"按钮,日期选择器将显示在视图的底部。但现实是simulation result

@IBAction func onUpdateBirthday(sender: UIButton) {
    showChooseBirthday()
}

func showChooseBirthday() {

    view.addSubview(chooseBirthday)

    let bottomConstraint = chooseBirthday.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor)
    let leftConstraint = chooseBirthday.leftAnchor.constraintEqualToAnchor(view.leftAnchor)
    let rightConstraint = chooseBirthday.rightAnchor.constraintEqualToAnchor(view.rightAnchor)
    let heightContraint = chooseBirthday.heightAnchor.constraintEqualToConstant(60)
    NSLayoutConstraint.activateConstraints([bottomConstraint,leftConstraint,rightConstraint,heightContraint])
    view.layoutIfNeeded() // re-layout the view

    self.chooseBirthday.alpha = 0
    UIView.animateWithDuration(0.4) {
        self.chooseBirthday.alpha = 1
    }

这是a part of storyboard

3 个答案:

答案 0 :(得分:1)

您应该使用textfield而不是button。并将pickerview设置为文本字段的inputview,这样当您在文本字段上点击时,它将会打开yourdatepicker而不是keyboard

如果您对此没有太多了解,可以使用Google提供许多教程。

在您的代码中,您应该将translatesAutoresizingMaskIntoConstraints设置为NO

希望这会有所帮助。

答案 1 :(得分:1)

正如其他人所说,您可以将日期选择器添加到UITextField。这是一个例子,我希望它对你有所帮助。 Result here

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var birthdayTextField: UITextField!
    var birthdayPicker = UIDatePicker()

    override func viewDidLoad() {
        super.viewDidLoad()
            configureBirthdayPicker()
        }

    func configureBirthdayPicker(){

        birthdayPicker.datePickerMode = .Date
        birthdayPicker.maximumDate = NSDate()
        birthdayPicker.tintColor = UIColor.blackColor()
        birthdayPicker.backgroundColor = UIColor.groupTableViewBackgroundColor()

        let toolBar = UIToolbar()
        toolBar.barTintColor = UIColor.groupTableViewBackgroundColor()
        toolBar.barStyle = UIBarStyle.Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor.blueColor()
        toolBar.sizeToFit()

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

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

        birthdayTextField.inputView = birthdayPicker
        birthdayTextField.inputAccessoryView = toolBar

    }

    func donePicker(doneclicked:UIButton){
        print("done clicked")
        birthdayTextField.resignFirstResponder()
    }

    func cancelPicker(cancelclicked:UIButton){
        print("cancel clicked")
        birthdayTextField.resignFirstResponder()
    }
}

答案 2 :(得分:0)

尝试将translatesAutoresizingMaskIntoConstraints更改为代码中的否