Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS objective-c中固定在键盘上的自定义UIView_Ios_Objective C_Keyboard - Fatal编程技术网

iOS objective-c中固定在键盘上的自定义UIView

iOS objective-c中固定在键盘上的自定义UIView,ios,objective-c,keyboard,Ios,Objective C,Keyboard,我想创建一个固定在键盘上的自定义UIView,我使用了“输入附件视图”,但当键盘不在时,它不会停留在我的视图控制器上。在iOS Objective-C中,我如何才能拥有一个已被限制在视图控制器底部的UIView,以粘贴到键盘上(如输入附件视图)?您需要将inputAccessoryView添加到文本字段中 范例 self.mytextField.inputAccessoryView = view; 希望这能解决您的问题为视图的底部约束添加一个IBOutlet。我假设它是ViewControll

我想创建一个固定在键盘上的自定义UIView,我使用了“输入附件视图”,但当键盘不在时,它不会停留在我的视图控制器上。在iOS Objective-C中,我如何才能拥有一个已被限制在视图控制器底部的UIView,以粘贴到键盘上(如输入附件视图)?

您需要将inputAccessoryView添加到文本字段中

范例

self.mytextField.inputAccessoryView = view;

希望这能解决您的问题

为视图的底部约束添加一个IBOutlet。我假设它是ViewController视图的直接子视图,当键盘被隐藏时,它应该位于底部

@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
订阅视图中的键盘通知将显示:

NotificationCenter.default.addObserver(self, selector: #selector(showKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(hideKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
在视图中取消订阅:

NotificationCenter.default.removeObserver(self)
稍后在ViewController中:

@objc func showKeyboard(_ notification: NSNotification) {
    let info = notification.userInfo
    let rectValue = info![UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
    if let keyboardSize = rectValue?.cgRectValue.size {
        bottomConstraint.constant = keyboardSize.height
        view.layoutIfNeeded()
    }
}

@objc func hideKeyboard(_ notification: Notification) {
    bottomConstraint.constant = 0
    view.layoutIfNeeded()
}
目标-C:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(void)keyboardWillShow: (NSNotification *) notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    _bottomConstraint.constant = keyboardSize.height;
    [view layoutIfNeeded];
}

-(void)keyboardWillHide: (NSNotification *) notification {
    _bottomConstraint.constant = 0;
    [view layoutIfNeeded];
}

这并不能解决问题。键盘未出现时,输入附件视图消失。我希望我的治疗出现在我最初放置它的地方,即使键盘没有显示。哦,我看到在这种情况下,你需要添加视图到你的底部。使用keyboardwillshow/keyboardwillHide通知,您需要更改该视图的位置。您可以从中获得帮助。如何使其像“输入附件”视图一样粘贴到键盘上?首选Objective-C。如果您可以编辑您的答案,请。@Omotayoolwatobi不客气;)如果出于某种原因不使用自动布局,则应手动更新框架,而不是更改bottomConstraint。谢谢