[英]UITableView Sections are not scrolling to top after ContentInset set
[英]UITableView Not Scrolling after contentInset
我正在開展一個項目,我有一個tableview和一個uitextfield。
當uitextfield獲得/放松焦點時,我正在應用以下方法:
-(void)enableInset {
CGFloat offSet = -30.0f;
UIEdgeInsets inset = UIEdgeInsetsMake(placesMapView.frame.size.height - offSet, 0.0f, 0.0f, 00.f);
// Updating the tableView position.
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -(placesMapView.frame.size.height - offSet));
placesTableView.scrollIndicatorInsets = inset;
}
和
- (void)disableInset {
CGFloat offset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
UIEdgeInsets inset = UIEdgeInsetsMake(offset, 0.0f, 0.0f, 00.f);
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -offset);
placesTableView.scrollIndicatorInsets = inset;
}
在viewDidLayoutSubviews中調用enableInset方法。 然后當我調用disableInset和enableInset時,UITableView不能再滾動了。
我做錯了什么? 知道我在哪里可以尋找答案嗎?
編輯:如果它可以幫助,我在github上添加了項目:
https://github.com/Loadex/PlaceViewer
要重新生成錯誤:滾動列表,點擊搜索欄,點擊取消,嘗試再次滾動列表。
奇怪地點擊過濾器按鈕,當UIActionSheet被解除時,滾動再次工作。
在尋找問題的解決方案時,我注意到您的placesTableView的contentInset的底部部分在不同的狀態下不斷變化。 在初始狀態下,當您可以看到地圖時,它為0,並且tableView的行為符合預期。 點擊搜索字段后鍵盤出現時設置為216。 我認為這是tableView和鍵盤之間的一些自動通信(通過Notifications或您在PlacesQueryTableViewController中執行的操作)。 這很好,因為我們希望底部插圖在出現時設置在鍵盤的頂部。 現在,這里出現了車輛部分。 當我點擊取消按鈕時,contentInset.bottom設置為-216。
我無法解釋為什么會發生這種情況,但我懷疑它與如何實現插入的自動更改有關。 我懷疑它確實像tableView.contentInset.bottom -= heightOfKeyboard
,這可能發生在動畫結束時,而不是之前。 您的問題的根源是您在動畫完成之前更改了contentInset的底部,因此在發生自動更改之前。 因此,一旦用戶點擊取消,您就將底部設置為0。 然后系統進入並通過鍵盤的高度減少它,結果是216.這就是我認為正在發生的事情。
要解決此問題,請避免更改contentInset的底部,只需更改頂部。 placesTableView.contentInset.top
是readOnly,但是如果你在下面的代碼中這樣做,你就可以解決這個問題。 我剛剛在每個方法中更改了兩行代碼,這些代碼與插件有關。 希望你看到我做了什么。
-(void)enableInset {
NSLog(@"Enabling insets");
// Setting the tableView to overlay the map view
CGFloat offSet = [placestableViewController tableView:placestableViewController.tableView heightForRowAtIndexPath:nil] - 30.0f;
UIEdgeInsets inset = placesTableView.contentInset; // UIEdgeInsetsMake(placesMapView.frame.size.height - offSet, 0.0f, 0.0f, 0.0f);
inset.top = placesMapView.frame.size.height - offSet;
// Updating the tableView position.
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -(placesMapView.frame.size.height - offSet));
placesTableView.scrollIndicatorInsets = inset;
placesMapView.hidden = NO;
[placestableViewController loadObjects];}
- (void)disableInset {
NSLog(@"Disable insets");
CGFloat offset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
UIEdgeInsets inset = placesTableView.contentInset;// UIEdgeInsetsMake(offset, 0.0f, 0.0f, 0.0f);
inset.top = offset;
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -offset);
placesTableView.scrollIndicatorInsets = inset;
// Hidding the map while in search
placesMapView.hidden = YES;}
。
順便說一句,如果你想知道我在不同的狀態下如何找到contentInset值,那就很簡單了。 我所做的是將自己設置為placesTableView的委托- (void)viewDidLoad
就像這樣placesTableView.delegate = self;
。 我還必須將@interface
語句更改為@interface KrackMapViewController () <UITableViewDelegate>
以表示我們符合UITableViewDelegate。 現在,這是訣竅:UITableViewDelegate符合UIScrollViewDelegate。 這意味着我們可以實現滾動視圖委托的方法。 特別有趣的是這一個:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { NSLog(@"Did scroll with insetTop: %f, insetBottom: %f, contentOffset: %f", placesTableView.contentInset.top, placesTableView.contentInset.bottom, placesTableView.contentOffset.y); }
這讓我們知道你何時開始拖動tableView,並在那里只是NSLog出不同的參數。
我希望這可以幫到你。 如果這對您有用,請告訴我。
經過一些研究后,我注意到了這一點:
釋放鍵盤時的TableView不會滾動,因為tableview似乎認為它顯示在整個屏幕上。 我試圖在tableview中添加更多數據,我們可以看到視圖滾動了一點。
我相信發生的事情是,當鍵盤被隱藏時,會進行一些自動調用並弄亂我在enableInset方法中設置的內容。 這是我的工作解決方案:
我注冊了hideKeyboard事件:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
在回調中我調用了enableInset:
- (void)keyboardDidHide: (NSNotification *) notif{
[self enableInset];
}
視圖再次向后滾動。 對此有任何解釋都是受歡迎的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.