syntagma
syntagma

Reputation: 24344

Adding UIScrollView to view and sending it to the back

I have added few UI components using Storyboard (UILabel, UIImageView). I am trying to put UIScrollView in this view, below all other elements:

Here is my code right at the end of -viewWillAppear method (code from my View Controller):

UIScrollView *scroll =  [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;

[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];

The reason why I need a UIScrollView is large amount of text in detailsLabel - I'm attaching code I use for handling this label programatically:

    detailsLabel.text = @"Some long text taken from databse";
    [detailsLabel sizeToFit];
    detailsLabel.numberOfLines = 0;
    detailsLabel.lineBreakMode = UILineBreakModeWordWrap;

What I am doing wrong? How do I put UIScrollView below all other elements of my main view?

Upvotes: 1

Views: 729

Answers (4)

Ajay Singh Azad
Ajay Singh Azad

Reputation: 74

Option One:

Declare detailsLabel in .h:

IBOutlet UILabel * detailsLabel; // MAP IT WITH NIB or STORYBOARD

give it property and Synthesise in .m.

here in viewDidLoad declare:

UIScrollView *scroll =  [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];

[scroll addSubview:detailsLabel];

// IT SHOULD WORK.

Option two:

If you have large amount of text to display, then you can also use UITextView, it has inbuilt scrollview. And you can also set its text edit property.

Upvotes: 0

syntagma
syntagma

Reputation: 24344

I've followed the instructions in this answer and it worked great: I've changed the Custom Class in Identity Inspector from UIView to UIScrollView and added this line to my -viewDidLoad in the View Controller file:

[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 700)];

Upvotes: 0

Mikhail Viceman
Mikhail Viceman

Reputation: 604

Try this:

UIScrollView *scroll =  [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;

[self.view insertSubview:scroll atIndex:0];

...

[self.view insertSubview:detailsLabel atIndex:1];

Upvotes: 1

IronManGill
IronManGill

Reputation: 7226

Put your code in ViewDidLoad . Then add the labels after you have added the UIScrollView to the self.View in the ViewDidLoad itself. So by default the UIScrollView would be added on the self.View and then your label. Now you can either add the label on the scrollview or onto the self.view .

Upvotes: 0

Related Questions