0
我已经创建了一个简单的扩展UISearchBar
,它比本机小一点。我已经实现了这个代码:如何从UISearchBar移动占位符标签
- (void)layoutSubviews{
[super layoutSubviews];
CGRect frame = self.frame;
if (frame.size.height != 32) {
frame.size.height = 32;
self.frame = frame;
}
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#636363"]}];
UITextField *searchTextField = [self valueForKey:@"_searchField"];
UILabel *placeholderLabel = [searchTextField valueForKey:@"_placeholderLabel"];
placeholderLabel.frame = CGRectMake(placeholderLabel.frame.origin.x , placeholderLabel.frame.origin.y + 8 , placeholderLabel.frame.size.width , placeholderLabel.frame.size.height);
[placeholderLabel setBackgroundColor:[UIColor redColor]];
[placeholderLabel setFont:[UIFont systemFontOfSize:13]];
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor colorWithHexString:@"#c6c6c6"]];
self.layer.borderWidth = 1;
self.layer.borderColor = [[UIColor colorWithHexString:@"#949494"] CGColor];
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:5];
}
但结果是这样的:
占位符标签仍然没有在视图的中心。而我无法弄清楚如何在那里移动它。字体也可以更改为背景颜色,但标签仍停留在顶部。
我已经实现了我自己的uisearchbar我修改了它。问题是为什么我无法修改占位符fram – Csabi
因为这是本地控制,并且标签的位置不能被修改。甚至不建议自己缩小控制范围。 如果你想要一个更窄的UISearchBar,标签位于中间,你应该在中间创建一个UITextField和一个占位符标签。 – dirtydanee