I need to make a custom search bar like this. The problem I am having is left aligning the placeholder text, as well as placing the search icon in the right. I have a png of the search icon that I have tried to use in an UIImageView
, and set that UIImageView
as the rightView
of the UISearchBar
's UITextField
. This solution has not worked, and I ran out of ideas. Does anyone have a solution?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
If you want your control to respond exactly how you want, you should probably make your own custom control. This control could be separated in three parts :
UIImageView
UITextField
UIButton
for the the search icon if you want the user to interact with itThe easiest way to do that is probably to create a new class
MySearchBar
, with the three parts in the private interface :In your
MySearchBar
, you can create your component, customize it, add a better look & feel. To get back the search result, your control can have a delegateid<UISearchBarDelegate>
(yourUIViewController
) which will basically simulate having a standard UISearchBar.What remains is to create your
MySearchBar
in your controller and set the delegate to your view controller. The messages from theUISearchBarDelegate
can either go to yourMySearchBar
to filter or do pre-treatment before sending to yourUIViewController
, or go directly to yourUIViewController
.Version for Xamarin
Don't use a
UISearchBar
if you need to do these kinds of customizations. You'll have to make your own using aUITextField
and aUIImageView
, and responding to the delegate calls.A working swift 3 solution for Drix answer: