将UISearchBar占位符对齐设置为NSTextAlignmentLeft

时间:2016-10-17 15:08:35

标签: ios objective-c swift uitextfield uisearchbar

我正在尝试在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]; 
  1. self.searchBar?.searchTextPositionAdjustment = UIOffset(horizontal: -100, vertical: 0)
    
  2. 但这些都没有奏效。还有其他我想念的东西吗?视图调试器显示文本字段居中,但更改其框架对我来说也没有用。

0 个答案:

没有答案