2015-07-22 80 views
2

我正在对应用程序进行简单的更改以添加UISearchBar组件,但在UI中出现了一些相当奇怪的布局怪癖。使用图片说明也许更容易。UISearchBar布局问题(iOS 8)

这里的组件看起来像当它没有焦点是什么:

UISearchBar default

注意,图标和占位符文本都太远了,而且占位符文本是太远了。而这里的时候我在现场挖掘会发生什么:现在

enter image description here

的图标是在正确的位置,但占位符文本是现在太靠左,并与图标重叠。是什么赋予了?

当该领域失去焦点时,它将恢复到其原始布局。除非我先输入一些文字,在这种情况下,它会保留第二个布局。

我使用的是成立UISearchBar没有做什么特别的代码:

UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
    containerView.backgroundColor = UI_COLOR_NAV_TITLEBAR; 

    searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width - 40, 44)]; 
    searchField.placeholder = @"search all items"; 
    searchField.searchBarStyle = UISearchBarStyleMinimal; 

    searchField.showsBookmarkButton = NO; 
    searchField.showsCancelButton = NO; 
    searchField.showsScopeBar = NO; 
    searchField.showsSearchResultsButton = NO; 

    UITextField* textSearchField = [searchField valueForKey:@"_searchField"]; 
    searchField.backgroundColor = [UIColor clearColor]; 
    textSearchField.textColor = UI_COLOR_NAV_TEXT; 

    searchField.delegate = self; 
    [containerView addSubview:searchField]; 

    //... 

我想UISearchBar总是出现在第二个例子中不与文本字段重叠,除了明显图标。有没有简单的解决方案,我忽略了这个问题?

+0

您是否找到解决方案? – someone

回答

0

尝试以下的searchField代码..

[searchField setLeftViewMode:UITextFieldViewModeNever]; 
[searchField setRightViewMode:UITextFieldViewModeNever]; 
[searchField setBackground:[UIImage imageNamed:@"searchicon.png"]]; 

最后是显示在左边的那个图标,使用图像。

+0

这有助于图标问题,但不会停止文本字段在焦点/不重点上移动。 – aroth

+0

我不认为有可能从uisearchbar删除图层动画。如果您尝试创建自定义搜索栏,这将很容易。 –