将DropDown放在UITextField上

时间:2019-02-23 19:00:04

标签: ios swift

所以我正在使用该框架进行DropDown:https://github.com/AssistoLab/DropDown

我正在尝试将DropDown放在我的UITextField上,但是不知何故它不允许我这样做。它只显示在左上角,一直到右侧。我试图这样设置:

func setupDropDown() {
        dropDown.anchorView = view
        dropDown.direction = .bottom
        dropDown.frame = CGRect(x: screenRect.size.width/2 - 30, y: 70, width: screenRect.size.width/2 + 10, height: 30)


        dropDown.dataSource = myArray

        dropDown.selectionAction = { [weak self] (index, item) in
            self!.exerciseNameTextField.text = "\(item)"
        }

        dropDown.cancelAction = { [unowned self] in
            self.dropDown.deselectRow(self.dropDown.indexForSelectedRow!)
        }
    }

无论我将dropDown.frame = CGRect(...)更改为什么,它都不会更改任何内容。有什么想法吗?

编辑: 当我尝试他们的演示时,它会附加在UITextField上,但不会结束:

func setupChooseArticleDropDown() {
        dropDown.anchorView = exerciseNameTextField

        // Will set a custom with instead of anchor view width
        //        dropDown.width = 100

        // By default, the dropdown will have its origin on the top left corner of its anchor view
        // So it will come over the anchor view and hide it completely
        // If you want to have the dropdown underneath your anchor view, you can do this:
        dropDown.bottomOffset = CGPoint(x: 0, y: exerciseNameTextField.bounds.height)

        // You can also use localizationKeysDataSource instead. Check the docs.
        dropDown.dataSource = [
            "iPhone SE | Black | 64G",
            "Samsung S7",
            "Huawei P8 Lite Smartphone 4G",
            "Asus Zenfone Max 4G",
            "Apple Watwh | Sport Edition"
        ]

        // Action triggered on selection
        dropDown.selectionAction = { [weak self] (index, item) in
            //self?.exerciseNameTextField.setTitle(item, for: .normal)
            self!.exerciseNameTextField.text = item
        }

        dropDown.multiSelectionAction = { [weak self] (indices, items) in
            print("Muti selection action called with: \(items)")
            if items.isEmpty {
                //self?.exerciseNameTextField.setTitle("", for: .normal)
                self!.exerciseNameTextField.text = ""
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您应该使用dropDown.width = 100进行设置。检查基本用法下的链接

DropDown Readme