iOS - 隐藏在键盘下的UIAlertController

时间:2016-03-04 15:10:26

标签: ios objective-c keyboard uialertcontroller

我有一个74 dfgfdggdf5455 68 sdffcc33 76 adsadsd333 86 hjjk khjjk 73 cdsc4344 63 aaaaxxxxsssxxx AND CONTINUE... 来自UIAlertController,该UIViewController被推送到UINavigationController作为最后UIViewController。此UIAlertViewController中包含UITextField

我的问题是,当我选择键盘显示的UITextField时,UIAlertViewController会保持居中,部分隐藏在键盘下方。

我的代码如下所示:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Enter Your PIN" message:@"Please enter your PIN!" preferredStyle:UIAlertControllerStyleAlert];

[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        [textField setSecureTextEntry:YES];
    }];

UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"Confirm"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {  }];


UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                         }];
[alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];

Screnshot Hidden UIAllertController

6 个答案:

答案 0 :(得分:5)

应该自动调整。 检查您查看层次结构

答案 1 :(得分:1)

如果我理解你,问题是键盘下会显示警报。我认为问题是你的viewController上显示了UIAlertController?但键盘是从rootViewController显示的。尝试使用此代码来管理问题

let alert = UIAlertController(title: "Title", message: NSLocalizedString("Message", comment: ""), preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default) { action in }
alert.addAction(okAction)
let rootViewController: UIViewController = UIApplication.shared.windows.last!.rootViewController!
rootViewController.present(alert, animated: true, completion: nil)

我写了扩展名,因为在某些情况下上面的代码无法正常工作

extension UIApplication {

class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
        return topViewController(base: nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
        let moreNavigationController = tab.moreNavigationController
        if let top = moreNavigationController.topViewController, top.view.window != nil {
            return topViewController(base: top)
        } else if let selected = tab.selectedViewController {
            return topViewController(base: selected)
        }
    }
    if let presented = base?.presentedViewController {
        return topViewController(base: presented)
    }
    return base
}

}

使用

    let alert = UIAlertController(title: NSLocalizedString(title, comment: ""), message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    UIApplication.topViewController()?.present(alert, animated: true, completion: nil)

答案 2 :(得分:1)

您可以添加[self.view endEditing:YES];以确保键盘不会显示。

答案 3 :(得分:0)

键盘显示在专用窗口中。所以是一个警报,你的viewcontrollers(见你的应用程序委托的window属性)等。windowLevel UIWindowUIAlertController个实例确定窗口的z顺序(即窗口是如何彼此上下相互展示。)

我们在应用程序中看到了相同的行为,我们使用自定义窗口进行自定义警报,该窗口始终显示在键盘的窗口下方。在iOS 9/10版本中,某些时候窗口级别的行为似乎发生了变化,因此键盘的窗口始终位于窗口堆栈的顶部。

由于view的窗口由系统管理,因此第三方开发人员可能无法修改此行为。

要检查警报窗口的窗口级别,您可以在呈现它之后引用其window的{​​{1}}的{​​{1}}属性(例如,在演示功能中完成块)。甚至可能会将其更改为一个非常大的值,以强制提示警报的窗口级别。

要跟踪此问题,您可以复制this radar

答案 4 :(得分:0)

我总结说这应该是iOS的一个bug,所以我决定通过创建一个普通的UIView来处理它。这是我创建的.xib文件,用于在其中使用UITextField模拟UIAlertcontoller。希望你的时间至少得到拯救。

https://www.dropbox.com/s/kg2nf9qcm4flhk0/AlertviewWithTextield.xib?dl=0

答案 5 :(得分:-3)

You can manage your keyboard based on textField location.


// Call this method somewhere in your view controller in **viewdidload** method

    - (void)registerForKeyboardNotifications
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(keyboardWasShown:)
                name:UIKeyboardDidShowNotification object:nil];

       [[NSNotificationCenter defaultCenter] addObserver:self
                 selector:@selector(keyboardWillBeHidden:)
                 name:UIKeyboardWillHideNotification object:nil];

    }

//Add in your View Controller which Called when the UIKeyboardDidShowNotification is sent.

    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;

        // If active text field is hidden by keyboard, scroll it so it's visible
        // Your app might not need or want this behavior.
        CGRect aRect = self.view.frame;
        aRect.size.height -= kbSize.height;
        if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
            [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
        }
    }
// Add in your View Controller which Called when the UIKeyboardWillHideNotification is sent

    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    }

Initialize UITextField * activeField;

Additional methods for tracking the active text field.

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        activeField = textField;
    }

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        activeField = nil;
    }

您还可以在Apple文档中查看这些概念。 https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html