此代码曾经有效,但在ios 10之后它已停止运行:
UITextField *searchTextField = [_searchBar valueForKey:@"_searchField"];
searchTextField.textAlignment = NSTextAlignmentRight
我尝试了段落和attributePlaceholder,但占位符总是左对齐。该错误出现在 xcode 7 和 8 中。我该如何解决这个问题?
也许这很长时间了,这是解决方案
斯威夫特
searchBar.semanticContentAttribute = .forceRightToLeft
如果您需要进行此类自定义,请不要使用
UISearchBar
。您必须使用 UITextField
和 UIImageView
自己制作,并响应 delegate
呼叫。
无论您是否在 NavigationBar 中使用 searchBar,此解决方案都适用于两种情况:
searchBar.searchTextField.semanticContentAttribute = .forceRightToLeft
searchBar.searchTextField.textAlignment = .right