当设备处于横向时,UISearchBar 不附带导航栏

Sus*_*tha 2 uisearchbar screen-orientation ios

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

 [videoSearchBar  setFrame:CGRectMake(0, 64, 320, 44)]; 
Run Code Online (Sandbox Code Playgroud)

对于 ios 7。

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

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

alb*_*amg 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;
}
Run Code Online (Sandbox Code Playgroud)