IOS: need to add the "Add" button in the search bar

I need a search control that allows the user to search for something, and then if he does not find it, he clicks the add button to add it. I need an add button to display, while the user enters the text to search, I think the right of the text. If I click the button on the navigation bar, it will not be displayed during user search.

I want to use the search bar and search controller, because I need the functionality to display results by user type. The first answer here - Adding a button to the left of the UISearchBar - does not seem to work with the search display controller. When the user activates the search field, he disappears.

I had several rabbit paths on this:

The second answer here is Adding a button to the left of the UISearchBar - I recommend overriding the Bookmarks button. This is very smart and works, but I can’t figure out how to get the “official” image of the “Add” button to override. The simulator no longer has images (?), And I generally don't like the idea of ​​hardcoding something in my code. I would rather (re) use the Apple Add button icon, but I cannot figure out how to do this.

I played with creating a subclass of UISearchBar that loads from a nib that has a search bar and button, but I understand that if I go this way, I think I need to redefine the interface for UISearchBar and forward the whole method to call the “real” UISearchBar that I control. It seems rude.

I tried to just create a view that subclasses UISearchBar and simply dynamically adds a button during initialization. But along the way, I am very bogged down in limitations. The restrictions for the text field in the search bar are a couple of levels in the hierarchy of views, and I'm sure that there is at least one restriction that I will need to remove, agree? (since the text box is currently limited to expand to the end of the search string).

I also tried the same subclass and tried to just set the position of the controls in layoutSubviews, but when I do this, I have to position all the child controls in the search bar (including the Cancel button, etc.), and it seems wrong.

I am still in training mode and I am not in a busy schedule. I would like to do this in the “right” way. Meaning I would like to reuse existing code and functionality and just add an add button. What is the “right” way to approach this?

Thanks!

+4
source share

Source: https://habr.com/ru/post/1497613/


All Articles