scrollview仅显示最后添加的子视图

Mal*_*loc 7 objective-c uiscrollview ios

我UIScrollView对子视图的行为很奇怪,我的想法是以编程方式创建一个UIView带有自定义nib文件的实例,在我的情况下是一个表单,用模型类中的数据填充该表单,并将其添加为我的子视图UIScrollView.问题是当我处理多个子视图时,UIScrollView唯一保留最新的子视图,因此如果我创建了三个子视图,则scrollview将仅显示第三个(最新的)子视图.虽然页面控件设置为coreect数量的子视图(三个).

该项目太长了,所以我将尝试用相关代码解释我的问题:

- (void)viewDidLoad {

    NSArray *sorted = [appArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];//this array contains the data from model, I debugged that to make sure I got the exact data, no more no less :)


    //Loop all NSManaged objects, for example let's say I have 3 model objects, don't worry about how I get data etc, because I debugged all and maked sure all data objects number are exact, etc.
    for (App_Table *_appTable in sorted) {
    //This looped 3 times as expected, I debugged that also and maked sure on each iteration I got the data I expected to have
    //App_Table is a subclass of NSManagedObject, it's my model class and get its data from coredata file
    [self addMoreView];//Call this method will create a new subview and add it as subview to the UIScrollView, it will also update the page control, update the content size property, etc.

    AppTableView *_appTableView = (AppTableView *) [[self.scrollView subviews] lastObject];//Remember addMoreView method create a new instance of AppTableView and add it as subview for the UIScrollView property, then I get that subview to fill it with data here

    _appTableView.txtADDRESS.text = _appTable.address;//Fill in the form, no need to write all the form fields code because it's the same way.

    // Scroll To First Page...
    self.pageControl.currentPage = 0;
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, viewWidth, viewHeight) animated:YES];

    self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct
    self.scrollView.delegate = self;

    [super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)

好的,所以当我有三个子视图时,scrollview将加载三个子视图的宽度,如上面的行计算:

self.scrollView.contentSize = CGSizeMake(viewWidth*noOfItems, viewHeight);//Set the content size to the sum of subviews width, I also debugged that to check it's correct
Run Code Online (Sandbox Code Playgroud)

我可以滚动直到3个移动(这是子视图的数量)并且UIPageControl也设置为三个点,但只有一个子视图可见,只有我能看到的最新一个,其他两个子视图消失,滚动视图计算内容它们的大小,但它们不可见.有什么想法吗 ?感谢名单.

编辑:

值得注意的是,第一次编辑视图时,一切都很顺利,当我处理3个子视图时,它们都是可见的,但是当我转到另一个视图并返回到该视图时,只有最后一个子视图可见.

此外,我正在研究一个具有拆分视图的iPad项目.

编辑: 这是绘制新子视图的方法的代码UIScrollView

 -(IBAction) addMoreView
    {

        NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"AppTableView" owner:self options:nil];

        AppTableView *aView = [arr objectAtIndex:0];
        [aView setFrame:CGRectMake(startX, 0, aView.bounds.size.width, aView.bounds.size.height)];
        [self.scrollView addSubview:aView];
        self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width+aView.frame.size.width
                                             , self.scrollView.contentSize.height);


        startX = startX + aView.frame.size.width;//Update the X position, first 0, then 600, 1200, and so on.
        [self.scrollView scrollRectToVisible:aView.frame animated:YES];
        NSLog(@"%f",self.scrollView.contentSize.width);
        NSLog(@"%f",self.scrollView.contentSize.height);

    }
Run Code Online (Sandbox Code Playgroud)

假设我有3个子视图,上面的方法将被调用3次,因为它被放入for循环中.所以这是NSLogs上述方法的结果:

NSLog(@"%f",self.scrollView.contentSize.width);
NSLog(@"%f",self.scrollView.contentSize.height);

    First iteration:

    600.000000

    0.000000

    Second iteration:

    1200.000000

    0.000000

    Third iteration:

    1800.000000

    0.000000
Run Code Online (Sandbox Code Playgroud)

编辑:

rob mayoff建议的命令让我看到为什么会发生这种情况:

   |    |    |    | <AppTableView: 0x1eef4700; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 990; layer = <CALayer: 0x1eef4790>>

   |    |    |    | <AppTableView: 0x1ee3f380; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 991; layer = <CALayer: 0x1ee3f410>>

   |    |    |    | <AppTableView: 0x1ee56910; frame = (0 18; 600 430); autoresize = LM+RM+TM+BM; tag = 992; layer = <CALayer: 0x1ee3f410>>
Run Code Online (Sandbox Code Playgroud)

所有三个子视图都在相同的框架中绘制,因此它们在彼此之上,但是当我在运行时使用断点调试时,x正在改变,第一次是0,然后是600然后是1200,这使我认为它正确地绘制.如何修复,尤其是x值正在递增,那么问题是什么以及为什么它们仍然在相同的x坐标上绘制?

Mal*_*loc 1

我回来有点晚了,但最终我找到了解决问题的方法,所以认为分享它可以节省其他人的时间。

就我而言,滚动视图是 nib 文件中的自定义视图。因此,通过激活左侧和顶部的自动调整大小蒙版,该错误得到修复。

在此输入图像描述