如何删除调试器在 Visual Studio 中建议的约束

How to remove constraints suggested by debugger in Visual Studio

在学习 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 Studio 中的这些约束?按照下面的 gif,我只在肖像中设置了 4 个约束,在风景中设置了 4 个约束。如果我通过故事板删除其中一个,将会出现冲突,说明并非所有约束都已正确设置。正如您在故事板上看到的那样,当我从一个设备移动到另一个设备时没有出现冲突,但是当我模拟任何设备时,将其翻转为横向并翻转回纵向 searchBar 元素会在屏幕上跳起从其初始 center 位置开始。我假设这种行为是由我需要删除的约束引起的?任何建议将不胜感激,谢谢。

除了移除景观约束外,您还需要确保您的约束正确描述了您希望如何布置视图以及视图的大小。

如果可以,我会避免给视图任何明确的大小限制 (height/width),并允许根据定位确定大小。

为了使搜索栏居中并确保无论方向如何都居中,您只需定义以下约束:

  1. 在容器中水平居中(视图应在 x 轴上)
  2. 在容器中垂直居中(视图应在 y 轴上)
  3. 从容器中领先 Space(视图的宽度应该是多少)

前两个约束定义了 x 和 y 位置。第三个约束告诉 iOS View 应该与屏幕的左边缘 "x" 距离。由于 iOS 知道视图应该水平居中(第一个约束)并且与左边缘有 x 距离 space (第三个约束),它可以计算出视图需要多宽才能满足这两个条件同时约束。

我只放置了这三个约束并产生了以下结果(动画 gif):