2015-02-05 20 views
38

Nó hoạt động tốt với bàn phím mặc định, nhưng tôi không thể làm cho nó hoạt động với numpad.Làm cách nào để thêm nút "Xong" vào Numpad trong IOS 8 bằng Swift?

Bất kỳ ý tưởng nào?

+0

thấy liên kết này http://www.weheartswift.com/make-custom-keyboard-ios-8-using-swift/ – Deepesh

+2

Tôi không quan tâm đến việc tạo ra một bàn phím mới, tôi chỉ muốn thêm một nút để UIToolBar, và sau đó thêm nó vào textField như một inputAccessoryView, như ở đây: http://stackoverflow.com/a/11382044/3207979, nhưng tôi havent figured it yet, làm thế nào để làm điều đó trong swift .. –

+0

Thay vào đó của Bàn phím hoặc Thanh công cụ tùy chỉnh, chúng tôi có thể đạt được điều này bằng cách thêm nút tùy chỉnh vào bàn phím mặc định. Tham khảo [answer] này (https://stackoverflow.com/a/47286917/756941). – NAZIK

Trả lời

49

Theo như tôi biết, bạn không thể thêm nút Xong trên phần bàn phím; bạn có thể thêm inputAccessoryView vào UITextField hoặc UITextView (nếu đó là những gì bạn đang sử dụng).

Kiểm tra documentation for more info.

Chỉnh sửa: Kiểm tra this question for an example về cách thực hiện điều đó.

Chỉnh sửa 2: Tương tự như example in Swift.

Chỉnh sửa 3: Mã từ chỉnh sửa 2, vì liên kết có thể hết hạn.

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    //--- add UIToolBar on keyboard and Done button on UIToolBar ---// 
    self.addDoneButtonOnKeyboard() 
} 

//--- *** ---// 

func addDoneButtonOnKeyboard() 
{ 
    var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50)) 
    doneToolbar.barStyle = UIBarStyle.BlackTranslucent 

    var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction")) 

    var items = NSMutableArray() 
    items.addObject(flexSpace) 
    items.addObject(done) 

    doneToolbar.items = items 
    doneToolbar.sizeToFit() 

    self.textView.inputAccessoryView = doneToolbar 
    self.textField.inputAccessoryView = doneToolbar 

} 

func doneButtonAction() 
{ 
    self.textViewDescription.resignFirstResponder() 
    self.textViewDescription.resignFirstResponder() 
} 
+0

Hoạt động hoàn hảo. Triển khai nhanh chóng và dễ dàng. Cảm ơn! –

3

Trước hết hãy để tôi cho bạn biết một điều mà bạn có thể thêm nút Xong trên thẻ khóa mặc định của bạn. Thực ra hôm nay tôi vừa trải qua vấn đề này và giải quyết. Ready Made Code, chỉ cần đặt nó lên lớp .swift của bạn. Tôi đang sử dụng Xcode 7 và thử nghiệm điều này bằng cách sử dụng iPad Retina (ios 9).

Dù sao không nói nhiều hơn ở đây là mã.

//Creating an outlet for the textfield 
    @IBOutlet weak var outletTextFieldTime: UITextField! 

    //Adding the protocol of UITextFieldDelegate  
    class ReservationClass: UIViewController, UITextFieldDelegate { 

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    //Making A toolbar   
    let keyboardDoneButtonShow = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/17)) 
    //Setting the style for the toolbar 
    keyboardDoneButtonShow.barStyle = UIBarStyle .BlackTranslucent   
    //Making the done button and calling the textFieldShouldReturn native method for hidding the keyboard. 
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("textFieldShouldReturn:")) 
    //Calculating the flexible Space. 
    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    //Setting the color of the button. 
    item.tintColor = UIColor .yellowColor() 
    //Making an object using the button and space for the toolbar   
    let toolbarButton = [flexSpace,doneButton] 
    //Adding the object for toolbar to the toolbar itself 
    keyboardDoneButtonShow.setItems(toolbarButton, animated: false) 
    //Now adding the complete thing against the desired textfield 
    outletTextFieldTime.inputAccessoryView = keyboardDoneButtonShow 
    return true 

} 

//Function for hidding the keyboard. 
func textFieldShouldReturn(textField: UITextField) -> Bool { 
    self.view.endEditing(true) 
    return false 
    } 
} 

Đó là đem lại cho tôi những giải pháp như ...

enter image description here

hơn Một điều tôi chỉ muốn nói với bạn rằng đây là giải pháp làm việc mà tôi đã sử dụng trong dự án gần đây của tôi. Tôi đã đăng bài này bởi vì không có giải pháp làm việc có sẵn cho vấn đề này. Mã làm việc và giải thích như đã hứa.

EDIT: -

Làm cho nó chuyên nghiệp hơn ... Nếu bạn đang theo dõi các mã trên sau đó cũng sẽ cung cấp cho bạn các giải pháp làm việc nhưng ở đây tôi đang cố gắng để làm cho nó chuyên nghiệp hơn. Vui lòng lưu ý mã MOD. Tôi để lại mã không sử dụng trước đó với trích dẫn.

Changes ..

  1. Tạo một đối tượng Nút cá nhân
  2. Đặt nó vào thanh công cụ
  3. Giảm kích thước của thanh công cụ được tạo ra trước đó
  4. Định vị các nút tùy chỉnh sang trái của màn hình bằng cách sử dụng FlexSpace và NegativeSpace.

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    let keyboardDoneButtonShow = UIToolbar(frame: CGRectMake(200,200, self.view.frame.size.width,30)) 
    
    // self.view.frame.size.height/17 
    keyboardDoneButtonShow.barStyle = UIBarStyle .BlackTranslucent 
    let button: UIButton = UIButton() 
    button.frame = CGRectMake(0, 0, 65, 20) 
    button.setTitle("Done", forState: UIControlState .Normal) 
    button.addTarget(self, action: Selector("textFieldShouldReturn:"), forControlEvents: UIControlEvents .TouchUpInside) 
    button.backgroundColor = UIColor .clearColor() 
    // let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("textFieldShouldReturn:")) 
    let doneButton: UIBarButtonItem = UIBarButtonItem() 
    doneButton.customView = button 
    let negativeSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil) 
    negativeSpace.width = -10.0 
    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    //doneButton.tintColor = UIColor .yellowColor() 
    let toolbarButton = [flexSpace,doneButton,negativeSpace] 
    keyboardDoneButtonShow.setItems(toolbarButton, animated: false) 
    outletTextFieldTime.inputAccessoryView = keyboardDoneButtonShow 
    return true 
    } 
    

Nó hiện đang đem lại cho tôi những điều sau đây ... Hơn nữa cố gắng để phù hợp với hình ảnh và tìm thấy những thay đổi này.

enter image description here

Cảm ơn.

Hy vọng điều này sẽ hữu ích. Xin lỗi vì câu trả lời dài.

12

Bạn có thể thêm thanh công cụ bằng nút Xong vào bàn phím.InputAccessoryView thuộc tính của trường văn bản có thể được sử dụng để đặt thanh công cụ này.

Dưới đây là đoạn code mà tôi đã sử dụng trong trường hợp của tôi

//Add done button to numeric pad keyboard 
let toolbarDone = UIToolbar.init() 
toolbarDone.sizeToFit() 
let barBtnDone = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done, 
     target: self, action: #selector(VerifyCardViewController.doneButton_Clicked(_:))) 

toolbarDone.items = [barBtnDone] // You can even add cancel button too 
txtCardDetails3.inputAccessoryView = toolbarDone 

Screenshot

4

Nếu nút Done của bạn có nghĩa vụ phải chỉ cần đóng số và sau đó phiên bản đơn giản nhất sẽ được gọi là resignFirstResponder chọn như sau:

UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder)) 

Mã này hoạt động trong iOS9 và Swift 2, tôi đang sử dụng nó trong ứng dụng của mình:

func addDoneButtonOnNumpad(textField: UITextField) { 

    let keypadToolbar: UIToolbar = UIToolbar() 

    // add a done button to the numberpad 
    keypadToolbar.items=[ 
    UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder)), 
    UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil) 
    ] 
    keypadToolbar.sizeToFit() 
    // add a toolbar with a done button above the number pad 
    textField.inputAccessoryView = keypadToolbar 
} 
45

Một Swift-3 Version (dung dịch Marko của - mà làm việc cho tôi)

(trong trường hợp của tôi, tôi có một UITextField xác định là textfield)

func addDoneButtonOnKeyboard() { 
    let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50)) 
    doneToolbar.barStyle  = UIBarStyle.default   
    let flexSpace    = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) 
    let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.doneButtonAction)) 

    var items = [UIBarButtonItem]() 
    items.append(flexSpace) 
    items.append(done) 

    doneToolbar.items = items 
    doneToolbar.sizeToFit() 

    self.textfield.inputAccessoryView = doneToolbar 
} 

func doneButtonAction() { 
    self.textfield.resignFirstResponder() 
} 
0

Theo Marko Nikolovski, hãy trả lời

Đây là mục tiêu-C câu trả lời:

- (void)ViewDidLoad { 
[self addDoneButtonOnKeyboard]; 
} 

- (void)addDoneButtonOnKeyboard { 
UIToolbar *toolBarbutton = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; 
toolBarbutton.barStyle = UIBarStyleBlackTranslucent; 

UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonAction)]; 

NSMutableArray *items = [[NSMutableArray alloc] init]; 
[items addObject:barBtnItem]; 
[items addObject:done]; 

toolBarbutton.items = items; 
[toolBarbutton sizeToFit]; 

self.timeoutTextField.inputAccessoryView = toolBarbutton; 
} 

- (void)doneButtonAction { 
[self.view endEditing:YES]; 
} 
7

Swift 3 giải pháp sử dụng @IBInpectable và mở rộng. Một trình đơn thả xuống sẽ xuất hiện bên dưới Trình kiểm tra thuộc tính trong Bộ dựng giao diện cho mọi UITextField trong dự án.

extension UITextField{  
    @IBInspectable var doneAccessory: Bool{ 
     get{ 
      return self.doneAccessory 
     } 
     set (hasDone) { 
      if hasDone{ 
       addDoneButtonOnKeyboard() 
      } 
     } 
    } 

    func addDoneButtonOnKeyboard() 
    { 
     let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)) 
     doneToolbar.barStyle = .default 

     let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) 
     let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction)) 

     let items = [flexSpace, done] 
     doneToolbar.items = items 
     doneToolbar.sizeToFit() 

     self.inputAccessoryView = doneToolbar 
    } 

    func doneButtonAction() 
    { 
     self.resignFirstResponder() 
    } 
} 
+0

cảm ơn, nó rất hữu ích –

Các vấn đề liên quan