UITableViewWrapperView和UITableView大小因自动布局而异 [英] UITableViewWrapperView and UITableView size differs with autolayout

查看:130
本文介绍了UITableViewWrapperView和UITableView大小因自动布局而异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立聊天。一切似乎都很好,但我遇到了一些错误的问题。

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

我得到了带有UITextView栏的UIViewController,用于输入消息和UITableView。
他们在这个约束中:V:| - (64) - [chatTable] [sendMessageBar] - (键盘) - |
键盘未输出时 - 此约束的常量为 0 。键盘输出后 - 我将常量增加到键盘高度。

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.

当键盘没有输出时:

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)

当键盘出现时:

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)

因此,在我增加约束常量后,UITableViewWrapperView的大小与其superview - UITableView不同。有办法解决这个问题吗?我假设 UITableViewWrapperView 会根据 UITableView 更改其框架和边界,但它不会。

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 ?

添加:

经过一些更多的研究 - 它似乎发生在 viewWillLayoutSubviews viewDidLayoutSubviews 之间。这有点奇怪:

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")
}

所以似乎有些东西会弄乱 UITableViewWrapperView

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

推荐答案

以下修复了它:

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

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

我发现在viewWillAppear()中,插图是但是在viewDidAppear()中,它们被修改为显然偏移了导航栏等。这使得UITableViewWrapperView与UITableView不同。
我在自己的例程中更改了insets,以便更容易尝试从不同的地方调用它。 viewWillLayoutSubviews()让它在呈现之前被更改 - 将更改放在viewDidAppear()中会导致表格抖动。

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.

这篇关于UITableViewWrapperView和UITableView大小因自动布局而异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆