问题
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.
回答1:
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)
回答2:
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;
}
来源:https://stackoverflow.com/questions/11302357/im-trying-to-get-a-uilabel-to-scroll-inside-of-a-uiscrollview-but-it-doesnt-scr