iOS 7中的UISearchBar中的UITextField

前端之家收集整理的这篇文章主要介绍了iOS 7中的UISearchBar中的UITextField前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用其中的TextField来完成与UISearchBar相同的外观,如我的iOS 6应用程序.我试图用几种方式编码,还没有成功.问题是,自从iOS 7以来,我无法以任何方式更改TextField的框架.结果是,我的TextField占用了NavigationBar中的所有空间,并覆盖了右边的UIBarButtonItem(菜单按钮).见下图:

Screenshots http://oi44.tinypic.com/2ufewx0.jpg

iOS 6代码:这是我如何在iOS 6中编码,在那里我可以将TextFields框架设置为任何我喜欢的!

UITextField *sbTextField = (UITextField *)[searchBar.subviews lastObject];
[sbTextField removeFromSuperview];

CGRect rect = searchBar.frame;
rect.size.height = 32;
rect.size.width = 210;
sbTextField.frame = rect;

[sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];


UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc]  initWithCustomView:sbTextField];

[[self navigationItem] setLeftBarButtonItem:searchBarNavigationItem];

iOS 7中的代码上面的结果:![iOS 7 look]

iOS 7代码:iOS 7中的区别是,您需要使用subViews才能将UITextField添加到UISearchBar / UINavigationBar.通过这样做,我还没有改变它的框架.它目前正在与右边的menuButton重叠,该代码可以在下面的图片中看到…

UITextField* sbTextField;
CGRect rect = subView.frame;
rect.size.height = 32;
rect.size.width = 115;

for (UIView *subView in self.searchBar.subviews){
    for (UIView *ndLeveSubView in subView.subviews){

        if ([ndLeveSubView isKindOfClass:[UITextField class]])
        {

            sbTextField = (UITextField *)ndLeveSubView;
            sbTextField.backgroundColor =[UIColor whiteColor];
            UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField];
            sbTextField.frame = rect;
            self.navigationItem.leftBarButtonItem = searchBarNavigationItem;
            self.navigationItem.rightBarButtonItem =  menuButton;
            [sbTextField removeFromSuperview];

             break;
        }

    }

 }
 [self.searchBar reloadInputViews];

SO …是否可以以任何方式更改子视图的框架(TextField)? 原文链接:https://www.f2er.com/iOS/336722.html

猜你在找的iOS相关文章