在学习Xamarin.iOS的过程中,我已经挣扎了几个星期了。
我尝试在故事板上放置一个searchBar元素,并在纵向和景观模式中设置约束。一旦我认为一切都正确,我就会在visual studio调试器中得到以下警告:
2019-01-30 22:43:43.200763+0000 App4[3113:24693] [LayoutConstraints] 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.
(
"<_UILayoutSupportConstraint:0x600001378050 _UILayoutGuide:0x7fe7bf425640.height == 44 (active)>",
"<_UILayoutSupportConstraint:0x60000137a120 V:|-(0)-[_UILayoutGuide:0x7fe7bf425640] (active, names: '|':UIView:0x7fe7bf425460 )>",
"<_UILayoutSupportConstraint:0x6000013780f0 _UILayoutGuide:0x7fe7bf525ce0.height == 34 (active)>",
"<_UILayoutSupportConstraint:0x6000013780a0 _UILayoutGuide:0x7fe7bf525ce0.bottom == UIView:0x7fe7bf425460.bottom (active)>",
"<NSLayoutConstraint:0x600001362ad0 V:[_UILayoutGuide:0x7fe7bf425640]-(200)-[UISearchBar:0x7fe7bf421940] (active)>",
"<NSLayoutConstraint:0x600001363a70 V:[UISearchBar:0x7fe7bf421940]-(200)-[_UILayoutGuide:0x7fe7bf525ce0] (active)>",
"<NSLayoutConstraint:0x6000013787d0 'UIView-Encapsulated-Layout-Height' UIView:0x7fe7bf425460.height == 414 (active)>")
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600001363a70 V:[UISearchBar:0x7fe7bf421940]-(200)-[_UILayoutGuide:0x7fe7bf525ce0] (active)>我知道我需要按照调试器的建议删除这些约束之一,但是在用代码编写的Visual中,这些约束在哪里找到呢?我只有4个约束设置在肖像和4个约束设置在景观,如下面的gif。如果我通过故事板删除其中一个,就会发生冲突,说并不是所有的约束都被正确设置。正如您在故事板上看到的,当我从一个设备移动到另一个设备时,不会出现冲突,但是当我模拟任何设备时,将其翻转到横向,然后将其翻转回肖像-- searchBar元素从其初始的center位置跳到屏幕上。我假设这种行为是由我需要删除的约束造成的?任何建议都将不胜感激,谢谢。

发布于 2019-01-31 23:07:26
除了删除景观约束外,您还需要确保您的约束适当地描述您希望视图布局的方式以及它的大小。
如果可以的话,我避免给视图任何显式的大小约束(高度/宽度),并允许根据定位来确定大小。
为了使搜索栏居中,并确保它无论方向如何都是居中的,只需定义以下约束:
前两个约束定义了x和y的位置。第三个约束告诉iOS视图应该是"x“距屏幕左侧的距离。由于iOS知道视图应该是水平的(第一个约束),并且离左边有x个距离(第三个约束),所以它可以计算出视图需要多宽才能同时满足这两个约束。
我只设置了这三个约束,并产生了以下结果(动画gif):

https://stackoverflow.com/questions/54451028
复制相似问题