2

在导航栏下添加了 UISearch 栏。我用

 [videoSearchBar  setFrame:CGRectMake(0, 64, 320, 44)]; 

对于 iOS 7。

在横向模式下,导航栏和搜索栏之间存在间隙。在早期版本中,它在没有 setFrame 的情况下正确显示。

在搜索栏下方有一个表格视图。

4

1 回答 1

5

导航栏的高度在纵向和横向之间变化。使用topLayoutGuide定位您的搜索栏。您可以在 Interface Builder 中或以编程方式执行此操作:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    [self.view addSubview:searchBar];

    searchBar.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = @{@"v":searchBar,
                            @"topLayoutGuide":self.topLayoutGuide};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][v]" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:views]];
}

#pragma mark - UISearchBarDelegate

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}
于 2013-10-03T09:59:14.430 回答