【问题标题】:iOS UISearchBar height in landscape mode in navigationBar iPhoneiOS UISearchBar 在导航栏 iPhone 中的横向模式高度
【发布时间】:2012-02-22 06:38:40
【问题描述】:

我需要横向模式下的 searchBar 方面的帮助。我将它添加到 navigationBar titleView - 居中:

self.searchBarTop = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
self.navigationItem.titleView = self.searchBarTop;

在横向模式下,它太大了,无法正确放入:

有什么建议吗?

【问题讨论】:

    标签: iphone ios height uisearchbar landscape


    【解决方案1】:

    使用以下代码找到iphone的Orientation,然后设置SearchBar的frame。在ViewDidLoad中设置SearchBar(Portrait模式)的frame。

    - (void)viewDidLoad {
        [super viewDidLoad];
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
        UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
        searchBar.delegate = self;
        [searchBarView addSubview:searchBar];
        self.navigationItem.titleView = searchBarView;
    }
    
    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
        if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {     
    
            UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
            UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
            searchBar.delegate = self;
            [searchBarView addSubview:searchBar];
            self.navigationItem.titleView = searchBarView;
        }
        else
        {
            UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 480.0, 44.0)];
            UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 470.0, 44.0)];
             searchBar.delegate = self;
            [searchBarView addSubview:searchBar];
            self.navigationItem.titleView = searchBarView;
    
        }
    }
    

    它一定会对你有所帮助。

    【讨论】:

    • 不幸的是,这没有帮助。您总是将高度设置为 44 磅。但是横向模式下的 navigationController 的高度为 32 点。像这样,我需要 searchBar 的高度不超过 30 点(顶部/底部边距 1 点)。
    • 在此代码行 UIView 的 if else 部分中更改您想要的 searchBar 的高度 *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 470.0, 44.0)];
    • 不,这也无济于事。显然 searchBar 的高度无法改变...
    【解决方案2】:

    显然这是自定义 UISearchBar 的灰色区域。我设法使用另一种方式将 searchBar 移动到导航栏之外。

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 2018-02-12
      • 2013-11-19
      相关资源
      最近更新 更多