我有一个 UIButton 的自定义子类,我想在多个项目中重复使用它,其中一些使用界面构建器和情节提要,而其中一些可能不使用。它涉及在几个地方设置按钮的框架。
如果我添加
self.translatesAutoresizingMaskIntoConstraints = YES;
在初始化中,一切看起来和工作正常,但是,我得到:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
Will attempt to recover by breaking constraint…etc
登录到控制台。但是,如果我设置
self.translatesAutoresizingMaskIntoConstraints = NO;
按钮大小都不起作用,按钮完全坏了。
有没有办法可以抑制错误,因为它似乎降级为正确的东西,或者我可以以某种方式手动删除有问题的约束?
我尝试在按钮初始化后转储约束,但是,它似乎不包含约束 AutoLayout 最终会破坏。
已编辑:以下是示例项目的链接,然后是我的按钮代码。在情节提要中关闭自动布局时效果很好,但是,当打开自动布局时,按钮似乎位于原点(似乎是因为调用 initWithCoder 时,按钮没有超级视图或任何位置信息)。