我是iOS UI开发新手。我陷入了下面的问题。Xamarin iOS Storyboard,子视图半宽,然后是父视图。查看
我想给父视图的一半宽度为2个子视图。 (见图)。我无法将乘数设置为小于零。红色和绿色子视图的宽度应该相等,宽度应该是父视图的一半。
我正在使用visual studio进行开发。
我是iOS UI开发新手。我陷入了下面的问题。Xamarin iOS Storyboard,子视图半宽,然后是父视图。查看
我想给父视图的一半宽度为2个子视图。 (见图)。我无法将乘数设置为小于零。红色和绿色子视图的宽度应该相等,宽度应该是父视图的一半。
我正在使用visual studio进行开发。
你应该能够乘数设定为1:2
,因为它与比的作品。
我觉得这是Visual Studio中的iOS设计器中的一个错误,因为乘数是一个浮点数。来自文档here:
乘数:属性2的值乘以该浮点数。在这种情况下,乘数是1.0。
在Xamarin Studio中,iOS的设计的比率被接受,而且1/2
0.5
:
而且在Xcode中:
它甚至被添加到源的xib/storyboard如下:
<constraint id="9" firstItem="3" firstAttribute="width" secondItem="8bC-Xf-vdC" secondAttribute="width" multiplier="1:2"/>
因此,您可以编辑.xib或.storyboard文件,但那不太理想。
来设定倍频的替代,你可以设置:
RedView
LeadingSpace到superview.leadingspace
TrailingSpace到superview.CenterX
GREENVIEW
LeadingSpace到superview.CenterX
TrailingSpace到superview.trailingspace
这将做一样的宽度设定到的所述宽度尺寸的一半上海华。
TBH Visual Studio和Xamarin工作室的iOS设计师与Xcode在增加复杂约束方面相比并不是很好(这并不复杂,但仍然失败)。如果可以,我会建议尝试在Xcode中进行编辑。
更新
如果您希望通过编程添加它们,在ViewDidLoad
补充一点:
public override void ViewDidLoad()
{
base.ViewDidLoad();
greenView.TranslatesAutoresizingMaskIntoConstraints = false;
redView.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddConstraints(new NSLayoutConstraint[]{
NSLayoutConstraint.Create(redView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, View, NSLayoutAttribute.Leading, 1, 0),
NSLayoutConstraint.Create(redView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1, 0),
NSLayoutConstraint.Create(redView, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1, 0),
NSLayoutConstraint.Create(greenView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1, 0),
NSLayoutConstraint.Create(greenView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, View, NSLayoutAttribute.Trailing, 1, 0),
NSLayoutConstraint.Create(greenView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, redView, NSLayoutAttribute.Top, 1, 0),
});
}
它将使像这样一个观点:
乘数不接受它。只接受整数值。你能否请你解释一下。 –
我更新了一些额外的信息 –
我的答案谢谢你的很好的解释。确实这是Visual Studio iOS Designer中的错误。我仍然有1个问题。我如何将trailingSpace设置为superview.CenterX?没有编辑约束的选项。我应该以编程的方式吗? 顺便说一句,我发现了另一种廉价的解决方案。 –