2017-01-10 67 views

回答

4

可以使用工具欄

override func viewDidLoad() { 
    super.viewDidLoad() 
    let tooBar: UIToolbar = UIToolbar() 
    tooBar.barStyle = UIBarStyle.BlackTranslucent 
    tooBar.items=[ 
     UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil), 
     UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed")] 
    tooBar.sizeToFit() 
    yourTextFeild.inputAccessoryView = tooBar 
} 

func donePressed() { 
    yourTextFeild.resignFirstResponder() 
} 

添加鍵盤上方完成按鈕它看起來像這樣

enter image description here

1

沒有工具欄,我會加入回車鍵地方本身完成按鈕通過以下步驟,

創建如下的自定義返回按鈕

let mobilePadNextButton = UIButton(type: UIButtonType.custom) 

在viewdidload中,設計它。在這裏,我正在設計Next按鈕,因爲我需要Next而不是完成。

mobilePadNextButton.setTitle("Next", for: UIControlState())//Set Done here 
mobilePadNextButton.setTitleColor(UIColor.black, for: UIControlState()) 
mobilePadNextButton.frame = CGRect(x: 0, y: 163, width: 106, height: 53) 
mobilePadNextButton.adjustsImageWhenHighlighted = false 
mobilePadNextButton.addTarget(self, action: #selector(self.mobilePadNextAction(_:)), for: UIControlEvents.touchUpInside) 

添加Keyboad通知

func textFieldDidBeginEditing(_ textField: UITextField) { 
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
} 


func keyboardWillShow(notification:NSNotification){ 

    if mobileNoTxtFld.isFirstResponder 
    { 
     DispatchQueue.main.async {() -> Void in 
      self.mobilePadNextButton.isHidden = false 
      let keyBoardWindow = UIApplication.shared.windows.last 
      self.mobilePadNextButton.frame = CGRect(x: 0, y: (keyBoardWindow?.frame.size.height)!-53, width: 106, height: 53) 
      keyBoardWindow?.addSubview(self.mobilePadNextButton) 
      keyBoardWindow?.bringSubview(toFront: self.mobilePadNextButton) 

      UIView.animate(withDuration: (((notification.userInfo! as NSDictionary).object(forKey: UIKeyboardAnimationCurveUserInfoKey) as AnyObject).doubleValue)!, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: {() -> Void in 
       self.view.frame = self.view.frame.offsetBy(dx: 0, dy: 0) 
      }, completion: { (complete) -> Void in 
       print("Complete") 
      }) 
     } 
    } 
    else 
    { 
     self.mobilePadNextButton.isHidden = true 
    } 

} 

在這裏,你可以做的做動作

func mobilePadNextAction(_ sender : UIButton){ 

    //Click action 

} 

用戶界面將類似於下,

enter image description here

我提到這個Git Code

相關問題