簡體   English   中英

如何刪除Visual Studio中調試器建議的約束

[英]How to remove constraints suggested by debugger in Visual Studio

在學習Xamarin.iOS的過程中,我一直在努力克服限制。

我嘗試將searchBar元素放置在情節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設備時,沒有沖突顯示,但是,當我模擬任何設備時,將其翻轉為橫向並翻轉回“縱向”,則searchBar元素將從其初始center跳到屏幕上位置。 我假設這種行為是由我需要刪除的約束引起的? 任何建議將不勝感激,謝謝。

在此處輸入圖片說明

除了消除景觀約束之外,您還需要確保約束條件正確描述了視圖的布局方式以及視圖的大小。

如果可以,我避免給視圖提供任何明確的尺寸限制(高度/寬度),並允許根據位置確定尺寸。

為了使搜索欄居中並確保其居中狀態而與方向無關,您只需定義以下約束:

  1. 在容器中水平居中(視圖應在x軸上)
  2. 在容器中垂直居中(視圖應在y軸上)
  3. 容器的前導空間(視圖的寬度應為多少)

前兩個約束定義x和y位置。 第三個約束條件告訴iOS,視圖應與屏幕左邊緣“ x”距離。 由於iOS知道視圖應水平居中(第一個約束),並且距左邊緣的距離為x距離(第三個約束),因此它可以計算出要同時滿足兩個約束所需的視圖寬度。

我僅放置了這三個約束,並產生了以下結果(動畫gif):

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM