私はこれを行う別の方法を見つけました。この方法論はどこにでも適用でき、スケーリングの問題はありません。マージンも処理します。それにサードパーティのものは必要ありません。
まず、このレイアウトを使用しないでください:
V:|-?-[Label1]-10-[Label2]-10-|
H:|-?-[Label1]-?-|
H:|-20-[Label2]-20-|
代わりにこれらを使用してください:
("|" is the real (outer) container)
V:|-?-[Label1]-0-[Label2HideableMarginContainer]-0-|
H:|-?-[Label1]-?-|
H:|-0-[Label2HideableMarginContainer]-0-|
("|" is Label2HideableMarginContainer)
V:|-10-[Label2]-10-|
H:|-20-[Label2]-20-|
では、今何をしましたか? Label2
はレイアウトでは直接使用されません。 余白コンテナ
に配置されます。そのコンテナは Label2
のプロキシとして、レイアウトでは0の余白で使用されます。実際のマージンは Margin-Container
のに配置されます。
Label2
を次のように隠すことができます:
そして
- Disabling the
Top
, Bottom
, Leading
そして Trailing
constraints. So seek them out, than set Active
to NO
on them. This will cause the Margin-Container
to have a Frame Size
of (0,0); because it does have subview(s); but there aren't any (active) layout constraints which anchors those subviews to it.
Maybe a bit complex, but you only have to develop it once. All the logic can be put into a separate place, そして be reused every time you need to hide smg.
以下はC#Xamarinのコードで、 Margin-Container
ビューの内側の端にサブビューを固定する制約をどのように探すかです:
public List SubConstraints { get; private set; }
private void ReadSubContraints()
{
var constraints = View.Constraints;//View: the Margin-Container NSView
if(constraints?.Any() ?? false)
{
SubConstraints = constraints.Where((NSLayoutConstraint c) => {
var predicate =
c.FirstAttribute == NSLayoutAttribute.Top ||
c.FirstAttribute == NSLayoutAttribute.Bottom ||
c.FirstAttribute == NSLayoutAttribute.Leading ||
c.FirstAttribute == NSLayoutAttribute.Trailing;
predicate &= ViewそしてSubviews.Contains(c.FirstItem);//ViewそしてSubviews: The View そして View.Subviews
predicate &= ViewそしてSubviews.Contains(c.SecondItem);
return predicate;
}).ToList();
}
}