iOS的:简单的自动版式1标签和按钮3 [英] iOS: Simple AutoLayout with 1 Label and 3 Buttons

查看:143
本文介绍了iOS的:简单的自动版式1标签和按钮3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩弄自动版式和我真的敲我的头撞在墙上了什么似乎像它应该是一个非常简单的解决方案。

I'm playing around with AutoLayout and am really banging my head against the wall for what seems like it ought to be an extremely simple solution.


  1. 我已经得到了控制垂直列:1,标签和3个按钮

  2. 我希望标签为40像素(点)高,自动调整大小宽度基于它的父的宽度(标准间距上左,上,右)。

  3. 我要的3个按键来排队垂直下方的标签。

  4. 我想它们的宽度来自动调整大小一样的标签。

  5. 我想他们的间距是标准(AQUA?)间距(8分吧?)。

  6. 我想了3个按钮是相同的高度。

我能得到什么,我希望发生的工作,但我不断收到错误控制台在运行时,我想弄清楚为什么我让他们和如何避免让他们。我看过的自动版式的WWDC视频,这里是我到目前为止已经试过:

I can get what I want to happen to work, but I keep getting errors in the console at runtime, and I'd like to figure out WHY I'm getting them and HOW to avoid getting them. I've watched the WWDC videos on AutoLayout, and here's what I've tried so far:

UILabel *label = [self Label];
MMGlossyButton *button1 = ...
MMGlossyButton *button2 = ...
MMGlossyButton *button3 = ...
[[self view] addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|"
    options:0
    metrics:nil
    views:NSDictionaryOfVariableBindings(label)]];
[[self view] addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button1]-|"
    options:0
    metrics:nil
    views:NSDictionaryOfVariableBindings(new)]];
[[self view] addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button2]-|"
    options:0
    metrics:nil
    views:NSDictionaryOfVariableBindings(existing)]];
[[self view] addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button3]-|"
    options:0
    metrics:nil
    views:NSDictionaryOfVariableBindings(provider)]];
[[self view] addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label(40)]-[button1(>=25)]-[button2(==button1)]-[button3(==button1)]-|"
    options:0
    metrics:nil
    views:NSDictionaryOfVariableBindings(label, button1, button2, button3)]];

所以,这适用于在动态大小的方式显示所述视图中,但下面的错误在控制台弹出

So, this works for displaying the view in a dynamically-sized way, but the following error pops up in the console:

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. 
        (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you 
               don't understand, refer to the documentation for the UIView 
               property translatesAutoresizingMaskIntoConstraints) 

// A whole bunch of constraint stuff that doesn't appear to be important...

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7554c40 V:[MMGlossyButton:0x7554b40(99)]>

所以,最后一位出现以指示该第一按钮我有是静态尺寸以99点高的的图。

这是其对视图的大小。

这完全是任意的。

我不希望分配,但不能想出一个办法来取消分配它。

So, the last bit appears to indicate that the first button I have on the view is statically sized to 99 points tall.
Which is the size it has on the view.
Which is completely arbitrary.
Which I don't want assigned, but can't figure out a way to un-assign it.

虽然我得到了我想要的(排序,最终),这似乎是实现东西是pretty简单的一个真正迂回的方式。我失去了对自动版式一些基本的东西,还是它的功率需要如此复杂?

Although I'm getting what I want (sort of, eventually), it seems to be a REALLY roundabout way to accomplish something that's pretty simple. Am I missing something basic about AutoLayout, or does its power require such complexity?

推荐答案

您遇到错误,因为你是混合和匹配在code。与由Interface Builder添加的约束产生约束。界面生成器不会让你产生暧昧的布局,因此几乎可以肯定,如果你增加额外的限制,你会得到一个无法同时满足的错误,这是许多倒台婚姻。

You are encountering errors because you are mixing and matching constraints generated in code with constraints added by interface builder. Interface builder doesn't let you generate an ambiguous layout, so almost by definition if you add additional constraints, you will get an "Unable to simultaneously satisfy" error, which is the downfall of many a marriage.

要解决这一点,你要么需要确定您在Interface Builder需要,或者您需要特定的那些标记为网点和添加您自己之前删除它们在code中的所有约束。

To resolve this you either need to define all the constraints you need in interface builder, or you need to mark specific ones as outlets and remove them in code before adding your own.

在您的情况下,约束是非常简单的IB创建。

In your case, the constraints are simple enough to create in IB.

您可以通过IB使用此按钮,选择您的标签钉到一个特定的高度:

You can pin to a specific height by using this button in IB while your label is selected:

一个在中间,看起来像一根大梁。这给你以下有用的菜单:

The one in the middle, that looks like a girder. This gives you the following useful menu:

选择其中的一个,您可以创建针对标签,然后你就可以通过选择编辑一个新的约束:

Choosing one of these allows you to create a new constraint against the label, which you can then edit by selecting it:

您可以再加入您的按钮,选择所有他们三人,并使用相同的菜单,创造一个平等的高度限制。

You can then add your buttons, select all three of them and, using the same menu, create an equal heights constraint.

在IB带来的限制不是特别灵活,所以如果你决定,你需要创建或修改它们在code,最好是建立网点的具体限制,然后要么删除并重新创建它们,或者在运行时修改值。

The constraints created in IB aren't particularly flexible, so if you do decide you need to create or modify them in code, it is best to create outlets to the particular constraints, and then either remove and re-create them, or modify the constant value at run time.

这篇关于iOS的:简单的自动版式1标签和按钮3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆