旋转方向时 UINavigationBar 中的 UISearchBar 更改宽度 [英] UISearchBar in UINavigationBar changes width when rotating orientation

查看:23
本文介绍了旋转方向时 UINavigationBar 中的 UISearchBar 更改宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯.我正在 iPad 上创建 splitview 应用.

This is driving me crazy. I am creating a splitview app on iPad.

我在 UINavigationBar 中创建了一个 searchBar - 像这样......

I have created a searchBar inside the UINavigationBar - something like this...

UISearchBar *searchBar = [[UISearchBar alloc] initWitFrame:CGRectZero];
[searchBar setPlaceHolder:@"Search"];
[searchBar sizeToFit];
[searchBar setDelegate:self];
[self.navigatioItem setTitleView:searchBar];

这对我来说很好用,像这样显示 UISearchBar...

this works just fine for me displaying the UISearchBar like this...

我也将此 UIBarButtonItem 设置为在侧窗格中显示主导航控制器.

I also have this UIBarButtonItem set to show Master navigation controller in sidepane.

当设备旋转到横向并且 UIBarButtonItem 被隐藏时会出现问题.然后searchBar就变成这样了...

The problem happens when the device rotates to landscape and the UIBarButtonItem gets hidden. Then the searchBar appears like this...

由于 barButtonItem 被隐藏并且无法更新搜索栏的大小,因此两侧似乎存在一些空间.

there is some space on sides which appears to be there since the barButtonItem got hidden and the size of search bar couldn't be updated.

此外,当我再次旋转设备时,UISearchBar 会发生这种情况

Furthermore, when I rotate the device again, here's what happens to UISearchBar

将搜索栏添加到 UINavigationBar 后,如何更新它的大小.即使使用 autoResizingMask 也不起作用.

How can I update the size of searchbar once it has been added to UINavigationBar. Even using autoResizingMask does not work.

searchBar.autoResizingMask =UIViewAutoresizingFlexibleWidth;

我在这里做错了什么?

推荐答案

我遇到了同样的问题(不是搜索栏,而是一些 UIView),我就是这样解决的:

I had about the same problem (it wasn't searchbar, but some UIView), and that's how I solved it:

尝试使用不会改变的固定大小创建它.我的意思是,如果您的导航栏最小尺寸是(例如)320pt,而 barbutton 大约是 30pt,并且您需要将搜索栏放置在中间(顺便说一句,这不是标准的,AFAIK),您必须将其设为 320- (30+10)*2 = 240pt.搜索栏将被放置在中间,并且有足够的空闲空间.

Try creating it with fixed size that won't change. I mean if your navbar minimum size is (for example) 320pt, and barbutton is about 30pt, and you need searchbar to be placed right in the middle (which, BTW is not standart, AFAIK), you'd have to make it 320 - (30+10)*2 = 240pt. Searchbar would be placed right in the middle and will have enough place left free.

这样既不会改变它的大小,也不会影响其他项目.

This way it will neither change it's size, nor it will affect other items.

或者我建议你把它放在正确的位置,而不是中间.

Or I would suggest you placing it at right position, not in the middle.

希望对您有所帮助.

这篇关于旋转方向时 UINavigationBar 中的 UISearchBar 更改宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆