UIPickerView 在 iPhone 上占据了所有水平空间,但在 iPad 上却没有?
我注意到一些以前从未出现过的问题。 我为 iPad 做了一个项目,其中使用了多个水平相邻放置的 UIPickerView
。在这里,它们尊重我初始化它们的 CGRect 框架,这意味着将其他元素放置在它们的两侧没有问题。
现在我尝试在 iPhone 项目上执行此操作,并且这里 UIPickerView
坚持作为唯一元素。它会自行调整大小以水平填充屏幕,并带有“周围”图形。
我尝试了不同的方法,将 UIPickerView 放置在不同的视图中,然后调整超级视图的大小,这只会导致剪切,而不是调整大小。另一件事是 UIPicker 坚持放置在屏幕中央。这基本上意味着,当将 UIPickerView
添加到屏幕时,即使其单个组件只有 70 px 宽,屏幕的 320 px 也会被用完。
我想要完成的是在屏幕右侧有一个 UIPicker 并在其左侧有一个按钮。
我在这里忽略了一些明显的事情吗?希望有人能伸出援手,提前致谢:)
没有什么比这更复杂的了:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 250.0f, 165.0f)];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:container.frame];
[picker setDelegate:self];
[picker setDataSource:self];
[container addSubview:picker];
我设定的框架不被尊重。它占据了所有水平空间。
I noticed something that has never been a problem before.
I did a project for iPad where I used several UIPickerView
positioned next to each other, horizontally. Here they respect the CGRect
frame I initialize them with, meaning placing other elements on either side of them was no problem.
Now I am trying to do this on an iPhone project and here a UIPickerView
insists on being the only element. It sizes it self to fill the screen horizontally, with the "around" graphics.
I tried different approaches, place the UIPickerView
inside a different view then sizing that super view, that just leads to clipping, not resizing. Another thing is that the UIPicker
insists on being placed in the center of the screen. This basically means that when a UIPickerView
is added to the screen, even though its single component is only 70 px wide, those 320 px of the screen is used up.
What I am trying to accomplish is to have a UIPicker
on the right side of the screen and a button to the left of it.
Am I overlooking something obvious here? Hope someone could lend a hand, thanks in advance:)
Nothing more complicated than this:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 250.0f, 165.0f)];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:container.frame];
[picker setDelegate:self];
[picker setDataSource:self];
[container addSubview:picker];
the frame I set, is not respected. It takes up all horizontal space.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了你的代码,结果相同。
但是,您可以在创建选取器并将其添加到容器后设置框架,并且会考虑新的大小。这是我的测试用例,它适用于我在 iPhone 模拟器中使用 SDK 4.2 的情况:
I tried your code with the same result.
However, you can set the frame after the picker has been created and added to your container, and the new size is respected. Here's my test case, which works for me using SDK 4.2, in the iPhone simulator: