我有一個包含聊天室的應用程序。每次有人輸入內容時,我都希望它向下滾動到輸入的最後一行。基本上,我希望鍵入的最後一個東西是屏幕上最低的東西。當我像十行聊天文本一樣打字時,它就會直接在我的視線下面。這裏是我的代碼:iPhone - 在聊天室向下滾動線條
- (void)scrollToBottom
{
CGRect lastLine;
lastLine.origin.x = 0;
lastLine.origin.y = self.contentSize.height-1;
lastLine.size.height = 1;
lastLine.size.width = 1;
[self scrollRectToVisible:lastLine animated:NO];
}
我把這個使用此代碼發佈的內容後:
- (void)displayChatMessage:(NSString*)message fromUser:(NSString*)userName {
[chat appendTextAfterLinebreak:[NSString stringWithFormat:@"%@: %@", userName, message]];
[chat scrollToBottom];
}
當我打電話appendTextAfterLinebreak以和爲代碼是:
- (void)appendTextAfterLinebreak:(NSString *)text
{
self.text = [[self.text stringByAppendingString:@"\n"] stringByAppendingString:text];
}
您使用的是UITextView嗎? –
@LyndseyScott我編輯的帖子,包括進一步的代碼,如果這有幫助 – user2779450
更有幫助,但仍然不夠好......什麼樣的結構是「聊天」?你必須使用scrollview或textview,如果你能夠滾動和追加文本... –