iPhone 中导航栏横向模式下 iOS UISearchBar 的高度

发布于 2024-12-29 16:15:52 字数 320 浏览 0 评论 0原文

我需要横向模式下 searchBar 的帮助。我将其添加到导航栏标题视图 - 居中:

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

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

在此处输入图像描述

有什么建议吗?

I need help with searchBar in landscape mode. I am adding it to the navigationBar titleView - centered:

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

In landscape mode it is to big to fit in properly:

enter image description here

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2025-01-05 16:15:52

使用以下代码找到iphone的方向,然后相应地设置SearchBar的框架。在ViewDidLoad中设置SearchBar的框架(纵向模式)。

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

    }
}

它一定会对你有所帮助。

Use the following code to find the Orientation of the iphone and then set accordingly frame of SearchBar.Set the frame of the SearchBar (Portrait mode) in ViewDidLoad.

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

    }
}

It will surely help you.

寄居者 2025-01-05 16:15:52

显然这是定制UISearchBar的灰色地带。我设法使用另一种方式将 searchBar 移到导航栏之外。

Obviously this is gray area customizing UISearchBar. I have managed to use another way moving searchBar outside the navigationBar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文