12

我正在尝试使用其中的 TextField 完成与我的 UISearchBar 相同的外观,就像在我的 iOS 6 应用程序中一样。我尝试以多种方式对其进行编码,但尚未成功。问题是,自 iOS 7 以来,我无法以任何方式更改 TextField 的框架。结果是,我的 TextField 占据了 NavigationBar 中的所有空间并覆盖了右侧的 UIBarButtonItem(菜单按钮)。请看下面的图片:

截图

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 外观]

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];

在此处输入图像描述 所以...是否可以以任何方式更改子视图的框架(TextField)?:(

编辑

答案有点蹩脚。为了使代码在 ios7 中使用 TextField 右侧的按钮,必须将 TextField 设置为 navigationBar 的 titleView。在 ios 6 中不是这种情况。但是会有其他故障,不建议在 iOS7 的 searchBars 中使用 TextField。请改用 searchDispalyController。在下面查看我的答案

 self.navigationItem.titleView = sbTextField;
4

7 回答 7

13

在 iOS 7 中要访问文本字段,您必须在级别上再次重申。像这样更改您的代码

for (UIView *subView in self.searchBar.subviews){
    for (UIView *ndLeveSubView in subView.subviews){
    if ([ndLeveSubView isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)ndLeveSubView;
            break;
        }
    }
   }

但是清除 UISearchBar 的背景并在文本字段中设置搜索栏图标的最佳方法是:

[searchBar setBackgroundImage:[[UIImage alloc] init] ];//if you want to remove background of uisearchbar
UIImage *image = [UIImage imageNamed: @"search_icon.png"];
[searchBar setImage:image forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
于 2013-09-25T15:12:29.423 回答
13

你不应该在 iOS 7 中放一个UITextFieldUINavigationBar这个小部件已经由 Apple 提供。

在 iOS 7 中,您可以简单地使用 aUISearchDisplayController和 aUISearchBar并设置:

searchDisplayController.displaySearchBarInNavigationBar = YES

搜索栏将出现在您的 s 中UINavigationBar,并且它可以与其他 s 很好地配合,UIBarButtonItem而无需在原始 iOS 6 解决方案中使用所有技巧和手动调整框架大小。

需要注意的一件事 - 如果您要将其添加到仍支持比 iOS 7 更早的操作系统的项目中,您需要确保检查调用,否则您的应用程序在旧操作系统上运行时会崩溃。

if([searchDisplayController respondsToSelector:@selector(displaysSearchBarInNavigationBar)])
{
    searchDisplayController.displaysSearchBarInNavigationBar = YES;
}

请参阅 iOS 7 过渡指南的这一部分: https ://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/Bars.html

在 iOS 7 中,UISearchDisplayController 包含了 displaySearchBarInNavigationBar 属性,您可以使用它在导航栏中放置一个搜索栏,类似于 iPhone 上的 Calendar 中的那个:

另一个注意事项 - 您应该考虑继续迁移到 AutoLayout,这样您就不必进行所有繁琐的帧操作。Apple 推荐它,可能是有充分理由的(未来的设备更大屏幕......?)

于 2013-10-04T15:37:15.383 回答
2

试试这个,我不确定,但这应该像 iOS 7 搜索栏有子视图一样工作,并且在该子视图中有两个子视图,其中一个是 UITextField

UIView *searchbarview = [searchBar.subviews objectAtIndex:0];
UITextField *sbTextField = (UITextField *)[searchbarview.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];
于 2013-10-04T14:08:31.397 回答
2

使用您的目标框架创建一个 UIView *textFieldContainer,将您的文本字段添加到该 UIView,然后将该 textFieldContainer 添加为导航项。即,您的方法保持不变,只是文本字段位于容器内,您可以使用该容器。

于 2013-10-02T10:53:52.563 回答
2

iOS6 & iOS7 兼容方案:

- (void)setTextFieldAsDelegate:(UIView *)inputView {
    for (UIView *view in inputView.subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            searchBarTextField = (UITextField *)view;
            searchBarTextField.delegate = self;
            break;
        } else {
            [self setTextFieldAsDelegate:view];
        }
    }
}
于 2013-10-24T08:01:55.410 回答
0

迅捷解决方案

for subView in searchBar.subviews{
        for deeperView in subView.subviews{
            if let searchField:UITextField = deeperView as? UITextField{
                searchField.layer.borderWidth = 1.0
                searchField.layer.borderColor = UIColor(red: 134/255, green: 14/255, blue: 75/255, alpha: 1).CGColor
                searchField.layer.cornerRadius = 5.0
            }
        }
    }
于 2015-07-14T21:55:15.970 回答
-3

感谢 spotdog13。我终于设法通过以下方式使其适用于 iOS 7:

#define TABLE_BOTTOM_MARGIN 5
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

@interface HomeViewController ()

@end

@implementation HomeViewController

@synthesize searchBar;
@synthesize searchResults;

- (void)viewDidLoad
{
 if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {


    [searchBar sizeToFit]; // standard size
    searchBar.delegate = self;

    // Add search bar to navigation bar
    self.navigationItem.titleView = searchBar;
    }
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
// Manually activate search mode
// Use animated=NO so we'll be able to immediately un-hide it again
[self.searchDisplayController setActive:YES animated:NO];
// Hand over control to UISearchDisplayController during the search
// searchBar.delegate = (id <UISearchBarDelegate>)self.searchDisplayController;

return YES;
}

#pragma mark <UISearchDisplayDelegate>
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController
 // Un-hide the navigation bar that UISearchDisplayController hid
[self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController
                                           *)controller {
searchBar = (UISearchBar *)self.navigationItem.titleView;
// Manually resign search mode
[searchBar resignFirstResponder];
// Take back control of the search bar
searchBar.delegate = self;
}
于 2013-10-15T07:02:14.817 回答