Dynamically added routines are not displayed in UIScrollView

I have a lot of problems dynamically adding subviews to UIScrollView. Scrolling view works fine with content created in NIB, but since the displayed nested objects depend on the data in my application (a mixture of images, labels, radio buttons, etc.), I should be able to create and display them dynamically.

According to everything I read, it looks pretty simple on different sites and in the Apple documentation. In the view controller viewDidLoad, I added the following code,

UILabel *testLabel = [[UILabel alloc] init]; [testLabel setFrame:CGRectMake(50, 50, 100, 40)]; [testLabel setText:@"My Test label"]; [scrollView addSubview:testLabel]; [testLabel release]; 

The label will not appear in the scroll view, but if I add testLabel to self.view, it will appear (but not in the scrollable content). I even tried to add code to viewDidAppear in case I misunderstood the order of events without any luck.

When I checked the debugger, I noticed that the scroll address is 0x0, which I suppose for some reason means its zero, which explains why it doesn't work. I was on the assumption that if I linked this scrollView pointer to the actual scroll in IB, it would be automatically assigned the correct address. This is not true? If so, how do I get the address of the submission?

- UPDATE -
Thanks everyone for the feedback. I checked everything, as everyone suggested, and it was, of course, everything was right. I did not need to set the size of the content, since I had some other dummy labels (for checking the scroll operation) in the NIB. But I will remember this later :-)

Interestingly, by checking the code again and not making any changes, I ran it again and it just worked! Not sure why, but I will post the reason if I ever find out ...

+6
source share
3 answers

When you use scrollView, you need to set the content size by following these steps:

 scrollView.contentSize = CGSizeMake(@width,@height); 

In this case, the size should be greater than 50.50 if you want to see the shortcut

Hope this helps

+3
source

As described in your question, you are working with a scroll list that you added in XIB.

When you declare an exit as IBOultlet UIScrollView * scrlvDynamicContent;

and connect it to scrollview in the interface builder, ideally you will get a dedicated scroll after calling the viewdidLoad method.

so do something like

 -(void)viewDidLoad{ [super viewDidLoad]; [self createDynamicView]; } 

where you can create a dynamic view as follows

 -(void)createDynamicView{ CGFloat yOffset = 0; for (int i=0;i<5;i++) { yOffset += 5; UILabel* lblHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(8, yOffset, 310, 21)]; [lblHeaderTitle setTextAlignment:UITextAlignmentLeft]; [lblHeaderTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0f]]; [lblHeaderTitle setText:[currentDict valueForKey:@"TITLE"]]; [lblHeaderTitle setTextColor:[UIColor blackColor]]; [scrlvDynamicContent addSubview:lblHeaderTitle]; [lblHeaderTitle release]; //INCREMNET in yOffset yOffset += 25; [scrlvDynamicContent setContentSize:CGSizeMake(320, yOffset)]; } 

Just make sure that scrlvDynamicContent is connected correctly to its outlet installed by its file owner.

+2
source
 [scrollView setFrame:CGRectMake(0,0,320,460)]; [scrollView setContentSize:CGSizeMake(100, 40)]; [self.view addSubview:scrollView]; UILabel *testLabel = [[UILabel alloc] init]; [testLabel setFrame:CGRectMake(50, 50, 100, 40)]; [testLabel setText:@"My Test label"]; [scrollView addSubview:testLabel]; [testLabel release]; 
0
source

Source: https://habr.com/ru/post/898652/


All Articles