2016-02-17 104 views
1

我使用的UITextView在迅速和鍵盤消失我使用的UITextView委託方法消失鍵盤的UITextView

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { 
    if(text == "\n") { 
     textView.resignFirstResponder() 
     return false 
    } 
    return true 
} 

,但我不希望使用此委託方法返回鍵盤BCZ我想爲下一行使用返回鍵。 有沒有其他方法可以消失鍵盤? 由於某種原因,我不使用觸摸事件。

+0

您正在鍵盤外添加手勢來解除它。 –

+0

我用過,但由於某些原因我不wana使用手勢。因爲當我使用手勢時會禁用某些功能。 – Sahil

+0

請刪除「返回false」並嘗試。 –

回答

2

你不能同時擁有一個回報,鍵盤上的按鍵解僱,除非您使用自定義鍵盤添加做過工具條。自定義鍵盤可以通過設置inputViewproperty

更好的標準做法是在鍵盤的頂部有一個按鈕,讓用戶可以隱藏它。這可以通過爲UITextView分配inputAccessoryView來完成。更多here

0

您可以在keyboard.On該做,你可以辭職,你的鍵盤動作

+0

我也在想添加工具欄。但是不可能在鍵盤上添加返回鍵和下一行? – Sahil

+1

一次只能使用鍵盤上的一個鍵,其他方面則必須使用custome鍵盤(第三方鍵盤)。 。 。在我看來使用完成的工具欄。 。這十分完美。 –

1

嘗試使用工具欄

override func viewDidAppear(animated: Bool) { 

    ... 


    var toolBar = UIToolbar() 
    toolBar.barStyle = UIBarStyle.Default 
    toolBar.translucent = true 
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 

    var nextButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Done, target: self, action: "nextTextfield") 
    var previousButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "previousTextfield") 
    var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    toolBar.setItems([previousButton, spaceButton, nextButton], animated: false) 
    toolBar.userInteractionEnabled = true 
    toolBar.sizeToFit() 

    textField.delegate = self 
    textField.inputAccessoryView = toolBar 
} 

func nextTextfield() { 
    nextTextField.resignFirstResponder() 
} 

func previousTextfield() { 
    //if exist previous   
    //previousTextField.resignFirstResponder() 
} 
1

試試這個

self.view!.endEditing(true) 
0

同時適用於斯威夫特和客觀C.

使用IQKeboardManager類寫在下面將幫助你快速的代碼。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    // Override point for customization after application launch. 

    IQKeyboardManager.sharedManager().enable = true 
} 

下面給出的鏈接將幫助你。

https://github.com/hackiftekhar/IQKeyboardManager