force UITextView to scroll to the top

當畫面中的UITextView被塞入了大量文字,且文字數量多到畫面可以scroll
這時候就需要透過撰寫程式碼的方式,強迫畫面重新scroll至最頂端,作法如下

- (void)viewDidLoad {
    [super viewDidLoad];
   
    NSString *verylongText = @"very looooooooooooong text";
    [self.myTextView setText:verylongText];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    // make the UITextView scroll to top
    [self.myTextView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}

沒有留言: