2015-10-14 31 views
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]; 


} 

但結果是這樣的:

enter image description here

佔位符標籤仍然沒有在視圖的中心。而我無法弄清楚如何在那裏移動它。字體也可以更改爲背景顏色,但標籤仍停留在頂部。

回答

0

答案是添加到UISearchBar一個高度約束,並將其設置爲32.魔術完成,佔位符定位良好。

0

您將不得不實施自己的控件,因爲您將無法移動搜索佔位符。

+0

我已經實現了我自己的uisearchbar我修改了它。問題是爲什麼我無法修改佔位符fram – Csabi

+0

因爲這是本地控制,並且標籤的位置不能被修改。甚至不建議自己縮小控制範圍。 如果你想要一個更窄的UISearchBar,標籤位於中間,你應該在中間創建一個UITextField和一個佔位符標籤。 – dirtydanee