UIPickerView 的大小设置不正确

发布于 2024-11-15 15:47:09 字数 1433 浏览 3 评论 0原文

我需要显示一个非全屏视图,并且其下方有一个按钮和 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):

I need to show a view that is not full screen and has a button and pickerView under it.
I tried using this code:

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

and i get this (picker out of the screen and very large, not 100x100):

enter image description here

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

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

发布评论

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

评论(3

此生挚爱伱 2024-11-22 15:47:09

您要在容器中添加选择器视图,容器框架是:-(20,20,200,200)

使其(0,20,200,200)

you are adding you pickeer view in container and container frame is:-(20,20,200,200)

make it(0,20,200,200).

毁梦 2024-11-22 15:47:09

您将 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 个值设置高度。这会起作用的。

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

You are adding your UIPickerView as subview of "container"

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

this means pickerview takes its origin from container not from UIView if you want it to on right position do this :-

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

just focus on your origin and remember every subview takes it origin from its parent view.
and One more thing apple doesn't allow to change pickerview Height so you cant set it 100x100 you can just change of its width. UIPickerView supports only 3 height value and these are 216.0 , 180.0, 162.0 try to set height only from these 3 value. it will be work.

let me know if you have any query regarding this.

〆一缕阳光ご 2024-11-22 15:47:09

如果您想调整框架,请在视图控制器的 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
}

If you want to adjust the frame, do it in your view controller's viewWillAppear: method - you can adjust it there.

Make sure that you also implement widthForComponent: and the width of all your components combined is less then the frame width - picker inset. I use the following:

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