Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm trying to get a UILabel to scroll inside of a UIScrollView but it doesnt scroll

This is in my .m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.scrollView addSubview:self.contentView];
    self.scrollView.contentSize = self.contentView.bounds.size;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

    NSMutableArray *arr = [[NSMutableArray alloc]init];
    arr = [Singleton getArray];

    NSString *str = [arr componentsJoinedByString:@"\n"];
    summaryLabel.text = str;
}

This is in my .h

@interface TotalViewController : UIViewController
{
    UIScrollView *scrollView;
    UIView *contentView;

}
@property (nonatomic, retain) IBOutlet UIScrollView * scrollView;
@property (nonatomic, retain) IBOutlet UIView       * contentView;
@property (nonatomic,strong) IBOutlet UILabel       * summaryLabel;

My Label is connected to the View Controller, my contentView is connected to the View Controller, and my summaryLabel is connected to the View Controller. I need the label to scroll and it is not.

like image 383
Johnny Cox Avatar asked Jul 02 '12 23:07

Johnny Cox


People also ask

How do I make my UI view scrollable?

You cannot make a UIView scrollable. That's what UIScrollView is for. However if you are using storyboards you can try to add constraints to the view so when you rotate the device the content remains inside the viewable area. Sounds like you already have some constraints setup so I would just play around with them.

How do I set scroll view constraints?

You need two groups of constraints to layout a scroll view: constraints that fix the frame (size and position) of the scroll view relative to its superview. constraints that layout the content relative to the content area of the scroll view and constrain its size.


2 Answers

A really simple answer, if you just want a single scrollable label, would be to use UITextView instead (reference). Disable editing, and you get a scrollable label.

(Taken almost verbatim from: how to add a scroll function to a UILabel)

like image 61
Dawson Toth Avatar answered Sep 29 '22 12:09

Dawson Toth


UIScrollView won't scroll if it's contents are not larger than it's visible area. Considering you are assigning the text to the label after you've set contentSize of the scrollview, it is unlikely that this is happening correctly. I would try something like this...

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.scrollView addSubview:summaryLabel];

    .....

    summaryLabel.text = str;

    // Assuming your label has numberOfLines = 0, and you want to scroll vertical
    CGSize maxSize = CGSizeMake(summaryLabel.frame.size.width, CGFLOAT_MAX);
    CGSize labelSize = [summaryLabel sizeThatFits:maxSize];
    scrollview.contentSize = labelSize;
}
like image 26
Jerry Jones Avatar answered Sep 29 '22 12:09

Jerry Jones