How to hide the shortcut bar in iOS9

帅比萌擦擦* 提交于 2019-11-26 22:21:15

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 = @[];
OverD

In Swift 2.0

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

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.

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();
}

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

jones_corey

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

In Swift 3.0 and 4.0

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