How to hide the shortcut bar in iOS9

巧了我就是萌 提交于 2019-11-26 08:20:23

问题


I have a textfield with a hidden keyboard (since I\'m using it with bluetooth). However, in iOS9 the shortcut bar keeps appearing.

Is there a way to hide it too?

Thank you so much!


回答1:


You can pass your textfield name in place of userNameTextField for which you want to remove shortcut bar.

UITextInputAssistantItem* item = [userNameTextField inputAssistantItem];
item.leadingBarButtonGroups = @[];
item.trailingBarButtonGroups = @[];



回答2:


In Swift 2.0

if #available(iOS 9.0, *) {
    let item : UITextInputAssistantItem = yourTextView.inputAssistantItem
    item.leadingBarButtonGroups = []
    item.trailingBarButtonGroups = []
} else {
  // Fallback on earlier versions
}



回答3:


I had the same issue. And so starts a search of SO. So the above helped me out, but the whole, "if iOS9 thing" might be best framed like this:

if ([self respondsToSelector:@selector(inputAssistantItem)]) {
    // iOS9.
    UITextInputAssistantItem* item = [self inputAssistantItem];
    item.leadingBarButtonGroups = @[];
    item.trailingBarButtonGroups = @[];
}

Happily, I'd created a sub-class of a UITextField, (CHTextField) and was in use everywhere. So it was a very easy fix to whack this in the over-ridden "init" method.

Hope it helps.




回答4:


Alternatively, just create an extension for UITextField in Swift 2.0 like this.

extension UITextField
{
    public func hideAssistantBar()
    {
        if #available(iOS 9.0, *) {
           let assistant = self.inputAssistantItem;
            assistant.leadingBarButtonGroups = [];
            assistant.trailingBarButtonGroups = [];
        }
    }
}

Then you can just call hideAssistantBar() on any text field you like.

@IBOutlet weak var myTextField: UITextField?;

override public func viewDidLoad() {
    super.viewDidLoad();

    myTextField?.hideAssistantbar();
}



回答5:


An easy way to do this for all text fields in your app is to create a category on UITextInputAssistantItem and override the getters for leadingBarButtonGroups and trailingBarButtonGroups like this:

@implementation UITextInputAssistantItem (RemoveBars)

- (NSArray<UIBarButtonItemGroup *> *)leadingBarButtonGroups
{
    return @[];
}

- (NSArray<UIBarButtonItemGroup *> *)trailingBarButtonGroups
{
    return @[];
}

@end

This worked for me on iOS 9.x and 8.x, no need for any conditional code.

Be careful with this though, this overrides those properties for EVERYTHING that uses UITextInputAssistantItem




回答6:


Just to expand on the other answers here. I cobbled together some Swift 2.0 code that will loop through all subviews of a given view and disable the UITextInputAssistantItems for all UITextFields and UISearchBars.

func hideTheAssistantBar(view:UIView) {
    //Check this view
    for case let textField as UITextField in view.subviews {
        let item : UITextInputAssistantItem = textField.inputAssistantItem
        item.leadingBarButtonGroups = []
        item.trailingBarButtonGroups = []
    }
    for case let searchBar as UISearchBar in view.subviews {
        let item : UITextInputAssistantItem = searchBar.inputAssistantItem
        item.leadingBarButtonGroups = []
        item.trailingBarButtonGroups = []
    }

    //Now find this views subviews
    let subviews = view.subviews

    for subview : AnyObject in subviews {
        if subview.isKindOfClass(UIView) {
            hideTheAssistantBar(subview as! UIView)
        }
    }
}

You can then call this function passing in whatever view you would like to start at. I call this inside of my ViewDidLoad() method and pass in self.view like hideTheAssistantBar(self.view).

I actually went a step further for my needs and added this function to a helper class I use for common code. Therefore inside of my viewDidLoad() function I actually just call helper.hideTheAssistantBar(self.view) and then I don't have to put that function in every file.

Hope this helps someone coming along looking for an easy way to remove the assistant bar from all UITextFields and UISearchBars in one fail swoop.

Thanks to @Arkader for the swift code to recursively find all subviews. Swift List Subviews




回答7:


In Swift 3.0 and 4.0

self.textField.inputAssistantItem.leadingBarButtonGroups.removeAll()
self.textField.inputAssistantItem.trailingBarButtonGroups.removeAll()


来源:https://stackoverflow.com/questions/32606655/how-to-hide-the-shortcut-bar-in-ios9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!