我正在尝试在Objective-C或Swift for iOS 9及更高版本中设置占位符文本对齐方式。我尝试过以下方法: 1.
for (UIView *subview in self.subviews) {
for(id field in subview.subviews) {
if ([field isKindOfClass:[UITextField class]]) {
[(UITextField*)field setTextAlignment:NSTextAlignmentLeft];
}
}
}
2
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTextAlignment:NSTextAlignmentLeft];
3
UITextField *searchTextField = [self valueForKey:@"_searchField"];
UILabel *placeholderLabel = [searchTextField valueForKey:@"_placeholderLabel"];
[placeholderLabel setTextAlignment:NSTextAlignmentLeft];
self.searchBar?.searchTextPositionAdjustment = UIOffset(horizontal: -100, vertical: 0)
但这些都没有奏效。还有其他我想念的东西吗?视图调试器显示文本字段居中,但更改其框架对我来说也没有用。