【问题标题】:Size of UIPickerView is not set correctlyUIPickerView 的大小设置不正确
【发布时间】:2011-06-13 09:42:56
【问题描述】:

我需要显示一个非全屏视图,并且在其下有一个按钮和 pickerView。 我尝试使用此代码:

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20,20,200,200)];
        container.backgroundColor=[UIColor whiteColor];

        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        myButton.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y+5, 170, 20); // position in the parent view and set the size of the button
        myButton.titleLabel.textColor=[UIColor redColor];
        myButton.titleLabel.text=@"click me";
        //myButton.backgroundColor=[UIColor blueColor];
        //[myButton backgroundImageForState:<#(UIControlState)#>[UIImage imageNamed:@"iPhone_mainbutton_green.png"];
        // add targets and actions
        [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        // add a buttonview
        [container addSubview:myButton];

        UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];
        //piker.numberOfComponents=1;
        piker.showsSelectionIndicator=YES;
        //piker.delegate=self;
        //piker.dataSource=self;

        [container addSubview:piker];

        [myButton release];
        [piker release];

        [self.view addSubview:container];

我得到了这个(屏幕上的选择器非常大,不是 100x100):

【问题讨论】:

标签: iphone objective-c uipickerview frame


【解决方案1】:

你正在容器中添加你的pickeer视图,容器框架是:-(20,20,200,200)

让它(0,20,200,200).

【讨论】:

  • @DixieFlatline 我尝试调整 pickerView 的高度,但无法调整大小。
【解决方案2】:

您将 UIPickerView 添加为“容器”的子视图

UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];

这意味着pickerview从容器而不是从UIView获取它的起源,如果你希望它在正确的位置这样做:-

UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
[container addSubview:picker];

只关注你的起源并记住每个子视图都从其父视图获取它。 还有一件事苹果不允许改变pickerview高度,所以你不能将它设置为100x100,你可以改变它的宽度。 UIPickerView 仅支持 3 个高度值,它们是 216.0 , 180.0, 162.0 尝试仅从这 3 个值设置高度。它会工作的。

如果您对此有任何疑问,请告诉我。

【讨论】:

    【解决方案3】:

    如果您想调整框架,请在视图控制器的 viewWillAppear: 方法中进行 - 您可以在那里进行调整。

    确保您还实现了widthForComponent:,并且所有组件的组合宽度小于框架宽度 - 选择器插图。我使用以下内容:

    - (void)viewDidLoad{
        self.picker = [[[UIPickerView alloc] init] autorelease];
        [self.view addSubview:self.picker];  
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        self.settingsPicker.frame =  CGRectMake(0.0, 100.0, self.view.frame.size.width, 100.0);
    }
    
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
        CGFloat guessedPickerInsetWidth = 24;
        CGFloat pickerWidth = self.view.frame.size.width - guessedPickerInsetWidth;
        if (component == SettingsPickerFirstComponent) {
            return pickerWidth * 0.4; // make the first component 40%
        }
        return pickerWidth * 0.3; // only two others, make them 30% each
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      相关资源
      最近更新 更多