Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我已经将 UIView 子类化了,所以我可以在其中画一些线。在这个 UIView 中有一大堆按钮,我决定将它们创建为在绘制线条后调用 drawRect 的方法。它可以工作,但有时只有部分按钮呈现。如果我删除按钮创建,而是在子类 UIViews 父级中添加 UIButton 它工作正常。
在 drawRect 方法中添加子视图之类的事情是一个很大的不,还是我不应该在 UIView 中一起做?
是的,这是一个很大的不,不。drawRect 用于添加带有 CG 的东西,而不是视图。它被反复调用且不可预测。不过,将它们添加到 initWithFrame 应该是相当安全的。