我有一個視圖窗口,我在界面生成器中創建。我創建了一個UIScrollView,它填充整個窗口並將其他一些項目拖入其中,包括UITextView。我遇到的問題是,當我點擊寫入TextView時,鍵盤阻止了TextView的視圖,因此使用了ScrollView。UIScrollView無法正常工作
現在我已經搜索了很多,並認爲我知道我需要做什麼,但如果我正確地做正確的事情是另一回事。
我得到了反彈,那就是我可以拖動ScrollView中的所有東西,它會反彈回來。當我按下在TextView中寫入時彈出鍵盤,這將ScrollView縮小爲「屏幕大小」 - 「鍵盤大小」(我知道這是因爲我沒有實現「一旦我隱藏鍵盤時執行此操作」功能但是,當我隱藏鍵盤時,ScrollView現在會在鍵盤啓動的地方結束)。但即使視圖大小現在小於內容大小,它也不會滾動,只是繼續反彈。
下面您可以看到我正在使用的代碼。我在viewDidLoad中調用registerForKeyboardNotifications。
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification *)n
{
NSLog(@"WoopWoopWoop");
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the noteView
CGRect viewFrame = self.mainScrollView.frame;
viewFrame.size.height -= (keyboardSize.height);
mainScrollView.bounces = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.4];
[self.mainScrollView setFrame:viewFrame];
[UIView commitAnimations];
}
我試過的內容視圖設置爲
[mainScrollView setContentSize:CGSizeMake(3200.0,2300.0)];
但是這並沒有影響一些這樣的任意大小...
任何想法?
我不知道爲什麼或如何,但放棄後,我在界面生成器中塗鴉,突然它工作.... – Bjorninn 2011-06-08 15:18:47