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
it could be done from story board there is combo box with name semantic on attribute inspector of search bar if you set it to force right to left its done you have right align search bar and it has a similar thing for aligning from left
SWIFT 3
swift 3 does not allow to override placeholder property. This is the modified version of Drix answer.
and call the function as :
No need to any customization just do it...
searchbar has centeral text alignment for its place-hoder , so just give some big text. and if you text is small then just use some space after place-holder text.
It's too late, but if anyone is still wondering the solution, then you can follow this.
You can get the search field using above code. Now simply use the properties you want to use, like.
PS. Text alignment property is used for text and placeholder both. Thanks.
Based in Mohittomar answer, as asked by @DevC to add spaces to the end of the place holder, here the code in swift:
I subclass placeholder in the UISearchBar, after set the value, I check if the last character is a space. then get the size of one space and how many spaces I need to add to align left.
A working solution for Drix answer
This method appearanceWhenContainedInInstancesOfClasses is not available in iOS 8, there is a workaround for iOS 8 here