[英]Vertical scrolling not working in UIScrollView and iOS7 and Xcode 5 despite not using Autolayout
[英]UIScrollView not scrolling in iOS7 with autolayout on
我有一個UIScrollView,里面有6個文本字段,里面有一個按鈕。 scrollView中沒有足夠的內容使其滾動。
但是當鍵盤顯示時,我希望滾動視圖滾動,這樣用戶就不必關閉鍵盤以選擇鍵盤隱藏的另一個文本字段。
我正在使用iOS7並啟用了autolayout。
有什么建議?
我正在使用故事板,我所擁有的唯一代碼如下。
reg.h文件
interface registerViewController : UIViewController <UITextFieldDelegate, UIScrollViewDelegate>
為了使滾動視圖可滾動,內容大小必須大於滾動視圖的框架,因此滾動視圖可以滾動到某些內容。 使用setContentSize調整內容大小:
[scrollview setContentSize:CGSizeMake(width, height)];
在這種情況下,您應該調整view.frame.width,view.frame.height + keyboard_height的大小,然后在鍵盤出現后調整內容偏移量:
[scrollview setContentOffset:CGPointMake(0, 0 - keyboard_height)];
如果對於一些棘手的,與autolayout相關的原因,這仍然不能使視圖可滾動,請在viewDidLayoutSubviews中實現此setContentSize函數以覆蓋autolayout:
- (void)viewDidLayoutSubviews {
[scrollview setContentSize:CGSizeMake(width, height)];
}
編輯:要在解除鍵盤后重置滾動視圖,請將scrollview內容大小重置為scrollview的幀並將偏移量重置為零:
[scrollview setContentSize:CGSizeMake(scrollview.frame.size.width, scrollview.frame.size.height)];
[scrollview setContentOffset:CGPointZero];
PS要為內容偏移設置動畫,請使用:
[scrollview setContentOffset:offsetSize animated:YES];
UIScrollViews有一個contentInset屬性,您可以設置contentInset以在底部創建額外的空間以允許滾動而不更改contentSize。
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 100, 0.0);
scrollView.contentInset = contentInsets;
上面的代碼在底部添加了100點插入。
順便說一下,有關於此事的官方文件。 它解釋了你應該做的一切。 你可以在這里找到它。 您可以在“移動位於鍵盤下方的內容”部分找到您要查找的內容
嘗試
要將Scroll內容大小設置為等於創建的視圖,請添加以下行
添加以下行
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.scrollView.contentSize = self.mainView.frame.size;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.