UIView 布局子视图委托
我发现自己需要支持为 UIView 委派 layoutSubviews 的责任。这是为了避免必须创建一个特定的子类来实现layoutSubviews方法。我怀疑我发现自己想要这种替代方案的原因可能是因为我错过了一些基本的 UIKit 设计注意事项,或者?或者我应该继续创建自己的 UIView 子类来支持这种类型的布局委托?
I find myself wanting support for delegating the responsibility of layoutSubviews for a UIView. This to avoid having to make a specific subclass just to implement the layoutSubviews method. I'm suspecting the reason I found my self wanting this alternative might be because I've missed some fundamental UIKit design considerations, or? Or should I just go ahead and make my own UIView subclass with support for this type of layout delegation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您需要自定义布局时,您都会想要创建一个子类,但无法委托。这也是唯一明智的方法 - 如果您在超级视图中包含一组视图,则该视图应该控制其子视图布局以实现可维护性。
您也不必在layoutSubviews中进行视图布局 - 如果您不需要可重用的东西,您可以在创建超级视图时简单地创建布局,并在此时分配位置和大小。
You're going to want to create a subclass whenever you need a custom layout, there is no way to delegate. It's also the only sane way to do it- if you're containing a set of views in a superview, that view should control its subview layout for maintainability.
You also don't have to do view layout in layoutSubviews- you can simply create the layout when you create the superview, and assign positions and sizes at that point, if you don't need something reusable.