简体   繁体   English

Swift,如何将现有的uiview转换为uiscrollview?

[英]Swift, how to convert an existing uiview to uiscrollview?

I have a storyboard with just one view at the moment but i needed to add 5 more textfields. 我目前只有一个视图的情节提要,但我需要再添加5个文本字段。 So i like to enable the vertical scroll. 所以我喜欢启用垂直滚动。 I thought i just had to change the class of the viewcontroller from UIView to UIScrollView: 我以为我只需要将viewcontroller的类从UIView更改为UIScrollView:

在此处输入图片说明

Besides changes the class what else should i do so i can enable vertical scroll for this view?? 除了更改类外,我还应该怎么做才能为该视图启用垂直滚动? Only thing i have in viewDidLoad() 我在viewDidLoad()中只有的东西

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Set date to today
    let today = dateToday()
    dateInvoice.text = today
}

edit ok i got the scrolling part working, but the hide/dismiss keyboard part doesn't work anymore. 编辑好,我使滚动部分工作了,但是隐藏/关闭键盘部分不再起作用了。 How do i dismiss the keyboard if a user clicks outside a textfield in scrollview 如果用户在滚动视图中的文本字段外单击,如何关闭键盘

    //HIDE KEYBOARD
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}
//END HIDE KEYBOARD

Edit 2: I found a workable solution thanks to: Swift UIScrollView : keyboard doesn't dismiss interactively 编辑2:由于以下原因,我找到了一个可行的解决方案: Swift UIScrollView:键盘无法交互关闭

在此处输入图片说明

Select your textfields and then In menu: 选择您的文本字段,然后在菜单中:

Editor->Embed in->Scroll view 编辑器->嵌入->滚动视图

Then make the scrollview the right size and if you're using autolayout add the needed constraints. 然后,将滚动视图设置为正确的大小,如果您使用的是自动布局,请添加所需的约束。

You can't just magically turn a UIView into a UIScrollView. 您不能仅仅将UIView神奇地变成UIScrollView。 Either delete the UIView and replace it by a scroll view, or else (easier) keep the UIView and drag a scroll view into it (and pin it on all four sides). 要么删除UIView并将其替换为滚动视图,要么(更轻松)保留UIView并将滚动视图拖入其中(并将其固定在所有四个侧面上)。

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

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