我试图让一个UILabel滚动一个UIScrollView,但它不滚动

这是在我的

- (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; } 

这是在我的

 @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; 

我的标签连接到视图控制器,我的contentView连接到视图控制器,我的摘要标签连接到视图控制器。 我需要标签滚动,而不是。

一个非常简单的答案,如果你只是想要一个可滚动的标签,将是使用UITextView( 引用 )。 禁用编辑,你会得到一个可滚动的标签。

(几乎逐字从: 如何添加滚动function的UILabel )

如果内容不大于可见区域,UIScrollView将不会滚动。 考虑到在设置scrollview的contentSize后将文本分配给标签,这种情况不太可能发生。 我会尝试这样的事情…

 - (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; }