添加 IB 约束会破坏 UIPickerview - ObjC

发布于 2025-01-11 21:33:54 字数 4267 浏览 2 评论 0原文

我有一个使用界面生成器构建的视图。每当我在现有 IB 视图之上添加 pickerview(通过 IB 或以编程方式)时,pickerview 将无法工作。选择器视图中的元素将无法正确滚动...旋转轮总是弹回“0 位置”&无法选择任何内容。此外,滚轮不再“发出咔哒声”...并且滚轮可以水平和垂直移动...但无法选择任何内容。

我什至尝试将 pickerview 添加到操作表中......结果相同。

如果我完全删除视图中的所有约束,则选取器视图将按其应有的方式工作。

关于造成此问题的原因和/或如何解决有什么想法吗?

屏幕拍摄

这是代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    [self.view setBackgroundColor:[UIColor clearColor]];
    
letterArray = @[@"",@"A",@"B",@"C",@"D"];
    
    UIBarButtonItem *pBtn = [[UIBarButtonItem alloc] initWithTitle:@"picker"
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                                action:@selector(launchPicker:)];
    self.navigationItem.rightBarButtonItem = pBtn;
}

- (void)launchPicker:(id)sender {
    self.viewForMainPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 400, [[UIScreen mainScreen] bounds].size.width, 220)];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 44)];
    UIPickerview *mainPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 220)];
    
    UIFont *arrowfont = [UIFont boldSystemFontOfSize:40];
    UIFont *titlefont = [UIFont boldSystemFontOfSize:28];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"→"
                                                                style:UIBarButtonItemStylePlain
                                                               target:self
                                                               action:@selector(fakeEvent)];
    UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:@"Select Gender"
                                                              style:UIBarButtonItemStylePlain
                                                             target:nil
                                                             action:nil];
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:self
                                                                          action:nil];
    [doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                     [UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
                                     arrowfont, NSFontAttributeName, nil] forState:UIControlStateNormal];
    [title setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                     [UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
                                     titlefont, NSFontAttributeName, nil] forState:UIControlStateNormal];
    NSArray *btnArray = [[NSArray alloc] initWithObjects:flex,flex,title,flex,doneBtn,nil];
    
    mainPicker.delegate = self;
    mainPicker.dataSource = self;
    
    [toolBar setItems:btnArray];
    [viewForMainPicker addSubview:mainPicker];
    [viewForMainPicker addSubview:toolBar];

    [self.view addSubview:viewForMainPicker];
}

- (void)fakeEvent {
    NSLog(@"selected");
}

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

- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return letterArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return letterArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *newString = [NSString stringWithFormat:@"%@", letterArray[row]];
    NSLog(@"passing: %@",newString);
}

I have a view built with interface builder. Whenever I add a pickerview (either via IB or programmatically) on top of the existing IB view, the pickerview will not work. The elements within the pickerview won't scroll properly... the spinner wheel always bounces back to the '0 position' & nothing can be selected. Also, the wheel no longer 'clicks'... and the wheel moves in horizontally as well as vertically... but nothing can be selected.

I even tried to add the pickerview to an actionsheet... same result.

If I completely remove ALL of the constraints in the view, the pickerview works as it should.

Any ideas on what's causing this and/or how to fix?

screen shot

Here's the code:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    [self.view setBackgroundColor:[UIColor clearColor]];
    
letterArray = @[@"",@"A",@"B",@"C",@"D"];
    
    UIBarButtonItem *pBtn = [[UIBarButtonItem alloc] initWithTitle:@"picker"
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                                action:@selector(launchPicker:)];
    self.navigationItem.rightBarButtonItem = pBtn;
}

- (void)launchPicker:(id)sender {
    self.viewForMainPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 400, [[UIScreen mainScreen] bounds].size.width, 220)];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 44)];
    UIPickerview *mainPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 220)];
    
    UIFont *arrowfont = [UIFont boldSystemFontOfSize:40];
    UIFont *titlefont = [UIFont boldSystemFontOfSize:28];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"→"
                                                                style:UIBarButtonItemStylePlain
                                                               target:self
                                                               action:@selector(fakeEvent)];
    UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:@"Select Gender"
                                                              style:UIBarButtonItemStylePlain
                                                             target:nil
                                                             action:nil];
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:self
                                                                          action:nil];
    [doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                     [UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
                                     arrowfont, NSFontAttributeName, nil] forState:UIControlStateNormal];
    [title setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                     [UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
                                     titlefont, NSFontAttributeName, nil] forState:UIControlStateNormal];
    NSArray *btnArray = [[NSArray alloc] initWithObjects:flex,flex,title,flex,doneBtn,nil];
    
    mainPicker.delegate = self;
    mainPicker.dataSource = self;
    
    [toolBar setItems:btnArray];
    [viewForMainPicker addSubview:mainPicker];
    [viewForMainPicker addSubview:toolBar];

    [self.view addSubview:viewForMainPicker];
}

- (void)fakeEvent {
    NSLog(@"selected");
}

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

- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return letterArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return letterArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *newString = [NSString stringWithFormat:@"%@", letterArray[row]];
    NSLog(@"passing: %@",newString);
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文