Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewWrapperView and UITableView size differs with autolayout

Tags:

I am building a chat. Everything seem to be quite ok but I bumped into sort of 'buggy' problem.

i got UIViewController with UITextView bar for entering message and UITableView. They are in this constraint: "V:|-(64)-[chatTable][sendMessageBar]-(keyboard)-|". When the keyboard is not out - the constant of this constraint is 0. and after keyboard is out - i increase the constant to keyboard height.

when the keyboard is not out:

self.table.contentSize = (375.0,78.5)
self.table.bounds = (0.0,-490.0,375.0,568.5)
self.table.frame = (0.0,64.0,375.0,568.5)
self.table.subviews[0].frame (UITableViewWrapperView) = (0.0,0.0,375.0,568.5)
self.table.subviews[0].frame (UITableViewWrapperView) = (0.0,0.0,375.0,568.5)

and when the keyboard comes out:

self.table.contentSize = (375.0,78.5)
self.table.bounds = (0.0,-274.0,375.0,352.5
self.table.frame = (0.0,64.0,375.0,352.5)
self.table.subviews[0].frame (UITableViewWrapperView) = (0.0,-137.5,375.0,137.5)
self.table.subviews[0].frame (UITableViewWrapperView) = (0.0,0.0,375.0,137.5)

So the UITableViewWrapperView, after I increase constraints constant, differs in size to its superview - UITableView. Is there a way to fix this ? I would assume that UITableViewWrapperView would change its frame and bounds according to UITableView but it does not.

Any ideas where is the problem or how could I work around it ?

ADDING:

After some more research - it seems that it happens somewhere between viewWillLayoutSubviews and viewDidLayoutSubviews. It is kinda weird tho:

override func viewWillLayoutSubviews() {
    println("WrapperView Frame :991: \(self.table.subviews[0].frame)") \\ WrapperView Frame :991: (0.0,0.0,375.0,568.5)
    super.viewWillLayoutSubviews()
    println("WrapperView Frame :992: \(self.table.subviews[0].frame)") \\ WrapperView Frame :992: (0.0,0.0,375.0,568.5)
}

override func viewDidLayoutSubviews() {
    println("WrapperView Frame :6: \(self.table.subviews[0].frame)") \\ WrapperView Frame :6: (0.0,-137.5,375.0,137.5)
    super.viewDidLayoutSubviews()
    println(">> viewDidLayoutSubviews")
}

So it seems that something happens there that messes up the UITableViewWrapperView

like image 279
Katafalkas Avatar asked Dec 27 '14 20:12

Katafalkas


1 Answers

The following fixed it for me:

func fixTableViewInsets() {
    let zContentInsets = UIEdgeInsetsZero
    tableView.contentInset = zContentInsets
    tableView.scrollIndicatorInsets = zContentInsets
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    fixTableViewInsets()
}

I discovered that at viewWillAppear() that the insets were all 0. But at viewDidAppear(), they had been modified to apparently offset for navigation bar, etc. This makes the UITableViewWrapperView different from the UITableView. I changed the insets in its own routine so that it was easier to experiment with calling it from different places. The viewWillLayoutSubviews() let it get changed before being presented - placing the change in viewDidAppear() caused the table to jerk.

like image 115
anorskdev Avatar answered Oct 01 '22 13:10

anorskdev