iOS - 更改 UIPickerView 组件宽度

发布于 2025-01-06 10:19:28 字数 164 浏览 2 评论 0原文

我知道我可以使用委托方法 – pickerView:widthForComponent: 设置 UIPickerView 的组件宽度,但是如果我有 2 个组件,而我只想更改第一个组件的宽度width 并将第二个组件的宽度保留为默认值(iOS SDK 会计算出的值)。我将如何实现这一目标?

I know I can set the UIPickerView´s component width with the delegate method – pickerView:widthForComponent: but say if I have 2 components and I only want to change the first component's width and leave the second component's width to the default value (something that iOS SDK would figure out). How would I achieve this?

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

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

发布评论

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

评论(3

花辞树 2025-01-13 10:19:28

PickerView 根据选择器中有多少组件来划分组件。所以组件没有默认值。但你仍然可以尝试这个。

为此,请尝试按百分比样式设置组件宽度。

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2 ;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return 10 ;
    }
    else
    {
        return 10 ;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        return @"Apple" ;
    }
    else
    {
        return @"iPhone" ;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0)
    {
        return (self.view.frame.size.width * 55 ) / 100  ;
    }
    else
    {
        return (self.view.frame.size.width * 30 ) / 100  ;
    }   
}

这是屏幕截图:

在此处输入图像描述

PickerView divide your component on the basis of how much component there are in picker. so there are no default value for component . but you can still try this .

For that , try to set your component width by percentage style .

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2 ;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return 10 ;
    }
    else
    {
        return 10 ;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        return @"Apple" ;
    }
    else
    {
        return @"iPhone" ;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0)
    {
        return (self.view.frame.size.width * 55 ) / 100  ;
    }
    else
    {
        return (self.view.frame.size.width * 30 ) / 100  ;
    }   
}

Here is screenShot :

enter image description here

三寸金莲 2025-01-13 10:19:28

除非您使用 if 语句并手动设置默认值(在找出它是什么之后),如下所示:

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0)
        return customWidth;
    return defaultValue;
}

我不确定您是否可以仅为某些组件设置值,但让默认值接管其他组件。

Unless you use an if-statement and manually set the default value (after finding out what it is) like this:

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0)
        return customWidth;
    return defaultValue;
}

I'm not sure you can set the value for only some of the components but let the default take-over for others.

对岸观火 2025-01-13 10:19:28

这是一个迟来的答案,但对于未来的读者来说,显然没有办法返回“默认”宽度。定义 pickerView:widthForComponent: 方法时,您必须显式设置所有组件的宽度,而不仅仅是其中一些组件的宽度。

This is a late answer, but for future readers, apparently there is no way to return a "default" width. The moment you define the pickerView:widthForComponent: method, you'll have to explicitly set all the components' widths, not just some of them.

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