【问题标题】:Adding Customview to UIScrollview results in stuck screen when Scrolling将 Customview 添加到 UIScrollview 会导致滚动时卡屏
【发布时间】:2012-12-22 03:22:46
【问题描述】:

我正在动态添加自定义视图,但出现此异常

例外:;符号存根:getpid

ITMCustomView *cView = [[ITMCustomView alloc] initWithFrame:CGRectMake(187, 660, 400, 400)
                                                      andOrderedItem:@"dd"
                                                              andTag:self.tagNumber
                                                       withFoldArray:self.foldTypeArray
                                                       withRollArray:self.rollTypeArray];

    cView.delegate = self;
        //set properties of cView...like cView.txtName.text =@"John";

    //self.scrollView.contentSize = CGRectMake(187, 660, 400, 400).size;


    /*CGPoint scrollPoint = CGPointMake(0.0, (cView.frame.origin.y/800)*400);
    [self.scrollView setContentOffset:scrollPoint animated:YES];

    [self.scrollView addSubview:cView];

    CGFloat scrollViewHeight = 0.0f;
    for (UIView *view in self.scrollView.subviews)
    {
        scrollViewHeight += view.frame.size.height;
    }

    CGSize newSize = CGSizeMake(320,scrollViewHeight);
    //CGRect newFrame = (CGRect){CGPointZero,newSize};
    [self.scrollView setContentSize:newSize];
    NSLog(@"750 the tag number is %d",self.tagNumber);
    NSLog(@"the scroll view height is %f",scrollViewHeight);
    self.currentCGRectLocation = cView.frame;*/

上面有很多代码,我在 /* */ 中注释过。在此之前我也有一行

//self.scrollView.contentSize = CGRectMake(187, 660, 400, 400).size;

如果我取消注释上面的行,它就会开始出现异常。我在我的应用程序中的另一个地方看到了这段代码。当我第一次启动应用程序时,会调用此代码并且它工作正常。现在我将记录保存在此屏幕-A 上并进行其他计算(基本上转到另一个屏幕-B)并返回此屏幕并检索保存的记录并显示它,只需将其显示在屏幕 A 上然后我得到这个例外。我注意到的是我对滚动视图所做的任何事情都给出了例外。如果我省略这一行

[self.scrollView addSubview:cView];

在 cmets 之外,我可以看到自定义视图添加得非常好..但是我失去了滚动..屏幕卡住了..所以我认为问题与滚动视图有关。如果您需要任何内容​​,请告诉我更多信息。谢谢..

编辑:改成这个..仍然不起作用。

        ITMCustomView *cView = [[ITMCustomView alloc] initWithFrame:CGRectMake(187, 660, 400, 400)
                                                      andOrderedItem:@"New"
                                                              andTag:self.tagNumber
                                                       withFoldArray:self.foldTypeArray
                                                       withRollArray:self.rollTypeArray];

    cView.tag =self.tagNumber;
    NSLog(@"Assigned tag is %d",cView.tag);
    cView.delegate = self;

    CGPoint scrollPoint = CGPointMake(0.0, (cView.frame.origin.y/500)*400);
    [self.scrollView setContentOffset:scrollPoint animated:YES];

    [self.scrollView addSubview:cView];

    CGFloat scrollViewHeight = 0.0f;
    for (UIView *view in self.scrollView.subviews)
    {
        scrollViewHeight += view.frame.size.height;
    }

    CGSize newSize = CGSizeMake(320,scrollViewHeight);
    //CGRect newFrame = (CGRect){CGPointZero,newSize};
    [self.scrollView setContentSize:newSize];
    NSLog(@"the scroll view height is %f",scrollViewHeight);
    self.currentCGRectLocation = cView.frame;

  for (NSObject *newObj in cView.subviews)
    {
        if ([newObj isMemberOfClass:[UITextField class]])
        {
            UITextField *txtObj = (UITextField *)newObj;
            switch (txtObj.tag) {
                case 6: //Length
                    NSLog(@"6 item value is %@",txtObj.text);
                    txtObj.text = [soliI.length stringValue]; //[NSString stringWithFormat:@"%1.1f",length];
                    break;
                case 7: //Width
                    NSLog(@"7 item value is %@",txtObj.text);
                    txtObj.text = [soliI.width stringValue]; //[NSString stringWithFormat:@"%1.1f",width];
                    break;

                default:
                    break;
            }
        }
        else if ([newObj isMemberOfClass:[UITextView class]])
        {
            UITextView *txtView = (UITextView *)newObj;
            NSLog(@"the tag is %d",txtView.tag);
            if (txtView.tag == 17)
            {
                txtView.text = soliI.specialInstructions;
            }
        }
        else if ([newObj isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)newObj;
            NSLog(@"btn tag is %d",btn.tag);
            switch (btn.tag) {
                case 12:
                    [btn setTitle:@"Fold" forState:UIControlStateNormal];
                    break;
                case 13:
                    [btn setTitle:@"Roll" forState:UIControlStateNormal];
                    break;
                default:
                    break;
            }
}

【问题讨论】:

  • 为什么你的scrollView的contentView大小和cView的frame一样大?

标签: ios exception dynamic uiscrollview custom-view


【解决方案1】:

我不确定您的代码,实际发生了什么。但问题是 ScrollView 的 contentSize 需要两个参数,即您想要滚动的高度和宽度,因为它是 CGSize 的性质,与您使用的 CGRectMake 不同。

检查一下,可能会解决您的问题。

【讨论】:

  • 添加了一些代码。请检查一下。它仍然不起作用。我将其更改为将滚动视图大小设置为 CGSize。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多