简体   繁体   English

iOS无法同时满足loadView中的约束

[英]iOS Unable to simultaneously satisfy constraints in loadView

I'm getting the Unable to simultaneously satisfy constraints error. 我收到了无法同时满足约束的错误。

I don't get the error when the viewcontroller establishes itself using viewDidLoad but it does get the error when using loadView... Why is this happening? 当viewcontroller使用viewDidLoad建立自身时,我没有收到错误,但是在使用loadView时却得到了错误。为什么会这样?

I thought the only difference between loadView and viewDidLoad is that viewDidLoad occurs after loadView. 我认为loadView和viewDidLoad之间的唯一区别是viewDidLoad在loadView之后发生。 At least, that seems to be the going explanation... 至少,这似乎是正在进行的解释...

I don't know whether my answer would completely address your issue,but it might act as a starting point to resolve your issue. 我不知道我的答案是否能完全解决您的问题,但它可能是解决您的问题的起点。

There are quite a few points(you might be aware of) which needs to be noted before you use loadView : 在使用loadView之前,有很多要点(您可能会意识到):

  1. loadView is a method that gets called when view is loading, viewDidLoad is method that will be executed after the view is loaded. loadView是在加载视图时调用的方法, viewDidLoad是在加载视图后执行的方法。

  2. loadView is recommended when you are willing to create the view programatically instead of setting in xib file,other wise there is no point in using or calling loadView (It all depends on your application requirement though). 当你愿意创建视图编程,而不是在厦门国际银行文件中设置的,其他明智的有使用或调用的loadView(这完全取决于你的应用需求虽然)是没有意义的,推荐的loadView。

  3. Don't call super loadView if you initialise your view from story board or xib file. 如果从故事板或xib文件初始化视图,请不要调用super loadView

  4. If you initialise your view from story board, do not call [super loadView] and you must assign your rootView to self.view property, if you call [super loadView] inside the method, you better never override this method and put your code in viewDidLoad method.. 如果从故事板上初始化视图,请不要调用[super loadView]而必须将rootView分配给self.view属性,如果在方法内部调用[super loadView] ,则最好不要重写此方法并将代码放入viewDidLoad方法。

  5. If you are using xib file to set up the view,do the modifications of objects set up in viewDidLoad and don't call loadView ,if you are creating the view programatically,initialise the view in the loadView and do the additional set up in viewDidLoad . 如果使用xib文件设置视图,请对在viewDidLoad中设置的对象进行修改,并且不要调用loadView ,如果要以编程方式创建视图,请在loadView中初始化视图,并在viewDidLoad中进行其他设置。

The constraint error generally pops up in story board or xib file,when "Use AutoLayout" is selected,if you unselect it,the error disappears,since you are creating the view programatically in loadView,it is your responsibility to see to it that the auto layout option is disabled ie you need to make use of setTranslatesAutoresizingMaskIntoConstraints property by setting it to "NO". 约束错误通常会在故事板或xib文件中弹出,选择“使用自动布局”时,如果取消选择,错误就会消失,因为您是在loadView中以编程方式创建视图的,因此有责任确保自动布局选项被禁用,即您需要通过将setTranslatesAutoresizingMaskIntoConstraints属性设置为“ NO”来使用它。

You can also refer to some of related questions here and there which could well get you out of this issue. 您还可以在这里那里参考一些相关问题,这很可能使您摆脱此问题。

Thanks and happy coding :) 谢谢,祝您编程愉快:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM