简体   繁体   English

如何在不使布局不明确的情况下使用大于或等于对iOS AutoLayout约束进行编程?

[英]How can I program iOS AutoLayout constraints using Greater Than or Equal without making the layout ambiguous?

I am writing an iOS app using MonoTouch where I want to programmatically define all of my UIViews and the AutoLayout constraints that define their layout. 我正在使用MonoTouch编写一个iOS应用程序,我想以编程方式定义我的所有UIView和定义其布局的AutoLayout约束。 I am currently trying to create a Table-like collection of views (rows & columns, not a UITableView) and I am able to use AutoLayout constraints to adjust column and row "borders" or markers. 我目前正在尝试创建一个类似表的视图集合(行和列,而不是UITableView),并且能够使用AutoLayout约束来调整列和行的“边界”或标记。 It looks fine when I run the app, but I keep getting an ambiguous layout for the table (using the HasAmbiguousLayout field). 当我运行该应用程序时,它看起来不错,但是我一直在为该表获得一个模糊的布局(使用HasAmbiguousLayout字段)。 I have distilled it down to the fact that my border markers use GreaterThanOrEqual (GTE) constraints rather than Equal, which is required since I want to make sure my rows and columns fit an arbitrary number of views. 我将其简化为以下事实:我的边界标记使用的是GreaterThanOrEqual(GTE)约束而不是Equal,这是必需的,因为我想确保行和列适合任意数量的视图。

My understanding was than I can use GTE type constraints and the AutoLayout system will attempt to minimize the value so that it is still GTE (vice-versa for LTE constraints). 我的理解是,我可以使用GTE类型约束,而AutoLayout系统将尝试将该值最小化,使其仍为GTE(对于LTE约束,反之亦然)。 For example, I would expect that I can place a view's left egde GTE the right edges of 2 other views, so that the layout solver will determine the minimum value for the left edge such that it is still satisfying GTE the right edges of the other views. 例如,我希望可以将一个视图的左坐标GTE放置在其他两个视图的右边缘,这样布局求解器将确定左边缘的最小值,以便仍然满足另一个视图的右边缘的GTE。观点。

I created a simple example and I am not sure why I get an ambiguous layout. 我创建了一个简单的示例,但不确定为什么会得到模棱两可的布局。 I created a single-view project in MonoTouch and programmatically add a UIView box that serves as my container (which in more complex examples contains multiple rows and columns of views). 我在MonoTouch中创建了一个单视图项目,并以编程方式添加了一个UIView框用作我的容器(在更复杂的示例中,该视图框包含多行和多列视图)。 I then add only 1 subview to the box view, a UILabel called myLabel. 然后,我仅向框视图添加一个子视图,即一个名为myLabel的UILabel。 I add constraints to place the label within the box, place the box within the parent View, and set the box's right and bottom edges (this is the key part): 我添加了约束,以将标签放置在框内,将框放置在父视图内,并设置框的右边缘和下边缘(这是关键部分):

public partial class AutoLayoutTestViewController : UIViewController
{
    public AutoLayoutTestViewController () : base ("AutoLayoutTestViewController", null)
    {
        UIView box = new UIView ();
        box.BackgroundColor = UIColor.Green;
        box.TranslatesAutoresizingMaskIntoConstraints = false;
        View.AddSubview (box);
        View.AddConstraint (NSLayoutConstraint.Create (box, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1.0f, 50f));
        View.AddConstraint (NSLayoutConstraint.Create (box, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1.0f, 0f));

        UILabel myLabel = new UILabel ();
        myLabel.Text = "My Label";
        myLabel.BackgroundColor = UIColor.Red;
        myLabel.TranslatesAutoresizingMaskIntoConstraints = false;
        box.AddSubview (myLabel);
        box.AddConstraint (NSLayoutConstraint.Create (myLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, box, NSLayoutAttribute.Top, 1.0f, 10f));
        box.AddConstraint (NSLayoutConstraint.Create (myLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, box, NSLayoutAttribute.Left, 1.0f, 10f));
        // IMPORTANT CONSTRAINTS
        box.AddConstraint (NSLayoutConstraint.Create (box, NSLayoutAttribute.Bottom, NSLayoutRelation.GreaterThanOrEqual, myLabel, NSLayoutAttribute.Bottom, 1.0f, 10f));
        box.AddConstraint (NSLayoutConstraint.Create (box, NSLayoutAttribute.Right, NSLayoutRelation.GreaterThanOrEqual, myLabel, NSLayoutAttribute.Right, 1.0f, 10f));
    }

    public override void DidReceiveMemoryWarning ()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewDidLayoutSubviews ()
    {
        base.ViewDidLayoutSubviews ();
        foreach (UIView v in View.Subviews) {
            if (v.HasAmbiguousLayout) {
                Console.WriteLine ("WARNING: View {0} has ambiguous layout!", v);
            }
        }
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        // Perform any additional setup after loading the view, typically from a nib.

    }
}

The following screenshot shows that the box and label appear as I want, but when the view controller finishes laying out the subviews, I get the message that my box has ambiguous layout. 下面的屏幕截图显示了框和标签的显示方式,但是当视图控制器完成对子视图的布局后,我收到消息,显示框的布局不明确。 If I change the "important constraints" for the box's right and bottom edges to be Equal to rather than GreaterThanOrEqual then the layout is no longer ambiguous. 如果将框的右边缘和下边缘的“重要约束”更改为“等于”,而不是“ GreaterThanOrEqual”,则布局不再模棱两可。 Again, it is important that I be able to specify my constraints as GTE because I am setting my borders with respect to an arbitrary number of views in rows and columns, so as I add new views to the table, I make sure that my right edge is constrained to be GTE all of the views and similarly for the bottom edge. 同样,重要的是我必须将约束指定为GTE,因为我要针对行和列中任意数量的视图设置边框,因此在向表中添加新视图时,请确保我的权限正确。边缘被限制为所有视图的GTE,底部边缘也类似。 I cannot use strictly equal to in my app because I do not know which subview in the table will be the right-most or bottom-most view; 我无法在我的应用程序中使用严格等于,因为我不知道表中的哪个子视图将是最右视图或最底视图。 this is the reason for having GTE constraints. 这就是受到GTE约束的原因。

自动版面测试

My questions are: why can't I use GTE constraints here for my box view without getting an ambiguous layout? 我的问题是:为什么在我的方框图中不能使用GTE约束而又没有模棱两可的布局? If this simple example does not work, how then do you use GTE constraints at all (they would all be ambiguous, otherwise you would specify them as Equal in the first place)? 如果这个简单的示例不起作用,那么您将如何使用GTE约束(它们将是模棱两可的,否则首先将它们指定为Equal)?

Thanks for any help, 谢谢你的帮助,

Cam 凸轮

Generally I do the same kind of thing, use GTE to insure something is limited in one dimension, but then add another constraint with a lower priority that attempts to do something else, like center a view within another view. 通常,我做同样的事情,使用GTE来确保某事物在一个维度上受到限制,然后添加另一个具有较低优先级的约束来尝试做其他事情,例如将视图置于另一个视图的中心。 You have to get creative. 你必须要有创造力。 The best way I've found is using pencil and paper, sketch an example out, and try to construct where I need what constraints. 我发现最好的方法是使用铅笔和纸,勾勒出一个示例,然后尝试构建我需要什么约束的地方。

I do not believe the system can equally allocate inequalities - if you have one on the left edge and one on the right edge, what should it do? 我不认为该系统可以平均分配不平等-如果您在左边有一个,在右边有一个,那么该怎么办? I use inequalities to insure that some maximum is never exceeded, then lower priority ones to try and achieve some goal (ie centering). 我使用不等式来确保不超过某个最大值,然后使用较低优先级来尝试实现某些目标(即居中)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较没有if语句的两个数字(大于,等于或小于) - Comparing two numbers (Greater than, equal to or less than) without if statements 请指导我如何解决此错误。索引(从零开始)必须大于或等于零且小于参数列表的大小 - Please guide about this error how i can solve it.Index (zero based) must be greater than or equal to zero and less than the size of the argument list 如何在比较小于、大于或等于之间动态切换? - How to dynamically switch between comparing Less Than, Greater Than, or Equal To? 如何限制WPF程序仅在Windows 8和更高版本上运行 - How can I restrict WPF program to only run on Windows 8 and greater 如何在Gridview文本上使用大于? - How can I use greater than on Gridview text? 如何检查表示数字的对象是否大于另一个对象? - How can I check if an object representing a number is greater than another? Acttionscript中“如果大于,则等于”的简写? - Shorthand for “If greater than, then equal to” in Acttionscript? 如何使用Xamarin iOS后面的代码来设置自动布局的不同约束 - How to use code behind to set different constraints for Auto Layout using Xamarin iOS C#:使程序适应计数大于或等于50的标记数 - C# :Adapt a program to count the number of marks greater than or equal to 50 Double.Epsilon表示相等,大于,小于,小于或等于,大于或等于 - Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM