如何为此视图设置约束?

时间:2015-12-18 04:11:14

标签: ios swift uibutton constraints

我有这个观点,我根本无法得到正确的约束。我甚至尝试过堆栈视图,但它仍然无法得到它。请单击图像链接以查看布局的外观。

Picture of my current view

我需要帮助在故事板中设置正确的约束,并确保它是动态的(包括文本缩小到正确的屏幕大小)。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

所以你有七个按钮:

seven loose buttons

你显然希望顶部按钮自己排成一排,剩下的按钮分成三行。因此,我选择前两个并排按钮,然后单击画布底部的“嵌入堆栈视图”按钮。

creating first row of two

因为我已经手动将两个按钮放在一个水平的行中,Xcode会自动将它们放在水平堆栈视图中。

我重复其他两行两个按钮的过程。最终看起来像这样:

rows created

然后我选择顶部独立按钮和三个水平堆栈视图,然后再次单击“嵌入堆栈视图”按钮。这次,Xcode创建了一个垂直堆栈视图,它看起来像这样:

columns created

由于您希望顶部按钮居中,因此我在“属性”检查器中将垂直堆栈视图的对齐方式设置为“中心”:

centered alignment for vertical stack view

我想你希望整个排列居中,所以我在垂直堆栈视图上创建约束,使其在容器中水平和垂直居中:

centering vertical stack view in its container

您还需要在按钮之间填充,因此我选择所有堆栈视图并在“属性”检查器中设置填充:

set spacing

您没有解释如何选择按钮的大小。我只需选择顶部按钮并将其限制为100乘60:

fixing top button size

然后我将选择文档轮廓中的所有按钮并将它们约束为相等的宽度和高度:

all buttons equal size

当我这样做时,Xcode将垂直堆栈视图偏离中心,但我只是在文档大纲中选择它,然后选择编辑器>解决自动布局问题> (所有视图)更新帧以修复错位。它最终看起来像这样:

final layout

答案 1 :(得分:1)

我编辑了你的故事板。在这里我粘贴了xml.Take outles的第一个顶部按钮宽度和高度约束。然后通过代码更改动态行为。

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonHeightConstant;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //60 = gap between button and left right padding
    //140 = gap between buttons and top bottom padding
    self.buttonWidthConstraint.constant = (self.view.frame.size.width - 60)/2;
    self.buttonHeightConstant.constant = (self.view.frame.size.height - 140)/4;
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];

    }];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="JGf-ti-vAF">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="YlR-y8-Jbu">
            <objects>
                <viewController id="JGf-ti-vAF" customClass="ViewController" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="Abf-DD-SvM"/>
                        <viewControllerLayoutGuide type="bottom" id="j19-ef-uDb"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="qIt-ah-XBl">
                        <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="7Cn-GC-UIA">
                                <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                            </imageView>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="aHp-jP-RgJ">
                                <rect key="frame" x="226" y="45" width="148" height="101"/>
                                <color key="backgroundColor" red="1" green="0.087589855729999999" blue="0.067484365290000001" alpha="1" colorSpace="calibratedRGB"/>
                                <constraints>
                                    <constraint firstAttribute="width" constant="148" id="emB-rW-56o"/>
                                    <constraint firstAttribute="height" constant="101" id="g0F-rb-AVX"/>
                                </constraints>
                                <fontDescription key="fontDescription" name="Avenir-Book" family="Avenir" pointSize="18"/>
                                <state key="normal" title="Button one">
                                    <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </state>
                                <userDefinedRuntimeAttributes>
                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                        <real key="value" value="8.5"/>
                                    </userDefinedRuntimeAttribute>
                                </userDefinedRuntimeAttributes>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="8Rm-YT-wZ7">
                                <rect key="frame" x="20" y="166" width="148" height="101"/>
                                <color key="backgroundColor" red="1" green="0.087589855729999999" blue="0.067484365290000001" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" name="Avenir-Book" family="Avenir" pointSize="18"/>
                                <state key="normal" title="Button one">
                                    <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </state>
                                <userDefinedRuntimeAttributes>
                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                        <real key="value" value="8.5"/>
                                    </userDefinedRuntimeAttribute>
                                </userDefinedRuntimeAttributes>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="T5I-DX-agA">
                                <rect key="frame" x="432" y="166" width="148" height="101"/>
                                <color key="backgroundColor" red="1" green="0.087589855729999999" blue="0.067484365290000001" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" name="Avenir-Book" family="Avenir" pointSize="18"/>
                                <state key="normal" title="Button one">
                                    <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </state>
                                <userDefinedRuntimeAttributes>
                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                        <real key="value" value="8.5"/>
                                    </userDefinedRuntimeAttribute>
                                </userDefinedRuntimeAttributes>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="Its-1I-c8f">
                                <rect key="frame" x="432" y="287" width="148" height="101"/>
                                <color key="backgroundColor" red="1" green="0.087589855729999999" blue="0.067484365290000001" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" name="Avenir-Book" family="Avenir" pointSize="18"/>
                                <state key="normal" title="Button one">
                                    <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </state>
                                <userDefinedRuntimeAttributes>
                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                        <real key="value" value="8.5"/>
                                    </userDefinedRuntimeAttribute>
                                </userDefinedRuntimeAttributes>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="Wxo-Rh-KVV">
                                <rect key="frame" x="20" y="408" width="148" height="101"/>
                                <color key="backgroundColor" red="1" green="0.087589855729999999" blue="0.067484365290000001" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" name="Avenir-Book" family="Avenir" pointSize="18"/>
                                <state key="normal" title="Button one">
                                    <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </state>
                                <userDefinedRuntimeAttributes>
                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                        <real key="value" value="8.5"/>
                                    </userDefinedRuntimeAttribute>
                                </userDefinedRuntimeAttributes>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="UDX-S2-uRA">
                                <rect key="frame" x="432" y="408" width="148" height="101"/>
                                <color key="backgroundColor" red="1" green="0.087589855729999999" blue="0.067484365290000001" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" name="Avenir-Book" family="Avenir" pointSize="18"/>
                                <state key="normal" title="Button one">
                                    <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </state>
                                <userDefinedRuntimeAttributes>
                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                        <real key="value" value="8.5"/>
                                    </userDefinedRuntimeAttribute>
                                </userDefinedRuntimeAttributes>
                            </button>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="wordWrap" translatesAutoresizingMaskIntoConstraints="NO" id="t2m-9Y-3Gr">
                                <rect key="frame" x="20" y="287" width="148" height="101"/>
                                <color key="backgroundColor" red="1" green="0.087589855729999999" blue="0.067484365290000001" alpha="1" colorSpace="calibratedRGB"/>
                                <fontDescription key="fontDescription" name="Avenir-Book" family="Avenir" pointSize="18"/>
                                <state key="normal" title="Button one">
                                    <color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                </state>
                                <userDefinedRuntimeAttributes>
                                    <userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
                                        <real key="value" value="8.5"/>
                                    </userDefinedRuntimeAttribute>
                                </userDefinedRuntimeAttributes>
                            </button>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                        <constraints>
                            <constraint firstItem="Wxo-Rh-KVV" firstAttribute="leading" secondItem="t2m-9Y-3Gr" secondAttribute="leading" id="180-eR-Wwt"/>
                            <constraint firstAttribute="trailing" secondItem="7Cn-GC-UIA" secondAttribute="trailing" id="5nC-Gx-Fa1"/>
                            <constraint firstItem="aHp-jP-RgJ" firstAttribute="width" secondItem="T5I-DX-agA" secondAttribute="width" id="BY2-aC-Yd3"/>
                            <constraint firstItem="Wxo-Rh-KVV" firstAttribute="top" secondItem="UDX-S2-uRA" secondAttribute="top" id="CuU-5v-5rN"/>
                            <constraint firstItem="UDX-S2-uRA" firstAttribute="leading" secondItem="Its-1I-c8f" secondAttribute="leading" id="DlE-Nf-3io"/>
                            <constraint firstItem="j19-ef-uDb" firstAttribute="top" secondItem="7Cn-GC-UIA" secondAttribute="bottom" id="Fbp-KG-e6q"/>
                            <constraint firstItem="7Cn-GC-UIA" firstAttribute="leading" secondItem="qIt-ah-XBl" secondAttribute="leading" id="Jr4-7g-Upm"/>
                            <constraint firstItem="aHp-jP-RgJ" firstAttribute="centerX" secondItem="qIt-ah-XBl" secondAttribute="centerX" id="M5Z-AL-SGA"/>
                            <constraint firstItem="t2m-9Y-3Gr" firstAttribute="leading" secondItem="8Rm-YT-wZ7" secondAttribute="leading" id="NBt-0c-Fv9"/>
                            <constraint firstItem="aHp-jP-RgJ" firstAttribute="top" secondItem="Abf-DD-SvM" secondAttribute="bottom" constant="25" id="NqB-Cg-7cN"/>
                            <constraint firstItem="t2m-9Y-3Gr" firstAttribute="top" secondItem="8Rm-YT-wZ7" secondAttribute="bottom" constant="20" id="Q76-UZ-V5H"/>
                            <constraint firstItem="aHp-jP-RgJ" firstAttribute="height" secondItem="T5I-DX-agA" secondAttribute="height" id="T9R-Ug-Bub"/>
                            <constraint firstItem="aHp-jP-RgJ" firstAttribute="height" secondItem="8Rm-YT-wZ7" secondAttribute="height" id="UNQ-d0-l4z"/>
                            <constraint firstItem="t2m-9Y-3Gr" firstAttribute="height" secondItem="aHp-jP-RgJ" secondAttribute="height" id="Zer-FI-bBB"/>
                            <constraint firstItem="UDX-S2-uRA" firstAttribute="height" secondItem="aHp-jP-RgJ" secondAttribute="height" id="c56-IW-gRF"/>
                            <constraint firstAttribute="trailing" secondItem="T5I-DX-agA" secondAttribute="trailing" constant="20" id="dXU-Gh-eXL"/>
                            <constraint firstItem="t2m-9Y-3Gr" firstAttribute="width" secondItem="aHp-jP-RgJ" secondAttribute="width" id="e14-04-CG0"/>
                            <constraint firstItem="Wxo-Rh-KVV" firstAttribute="height" secondItem="aHp-jP-RgJ" secondAttribute="height" id="fiF-yH-qvW"/>
                            <constraint firstItem="Wxo-Rh-KVV" firstAttribute="width" secondItem="aHp-jP-RgJ" secondAttribute="width" id="jc3-oJ-ze2"/>
                            <constraint firstItem="UDX-S2-uRA" firstAttribute="width" secondItem="aHp-jP-RgJ" secondAttribute="width" id="kNm-AX-HVB"/>
                            <constraint firstItem="aHp-jP-RgJ" firstAttribute="width" secondItem="8Rm-YT-wZ7" secondAttribute="width" id="kRf-PB-mDD"/>
                            <constraint firstItem="t2m-9Y-3Gr" firstAttribute="top" secondItem="Its-1I-c8f" secondAttribute="top" id="kTF-qT-xAw"/>
                            <constraint firstItem="8Rm-YT-wZ7" firstAttribute="top" secondItem="T5I-DX-agA" secondAttribute="top" id="mbc-xn-Fgi"/>
                            <constraint firstItem="Its-1I-c8f" firstAttribute="height" secondItem="aHp-jP-RgJ" secondAttribute="height" id="ogn-ja-iwo"/>
                            <constraint firstItem="8Rm-YT-wZ7" firstAttribute="leading" secondItem="qIt-ah-XBl" secondAttribute="leading" constant="20" id="riZ-Om-LeS"/>
                            <constraint firstItem="Its-1I-c8f" firstAttribute="width" secondItem="aHp-jP-RgJ" secondAttribute="width" id="s0k-AT-xQk"/>
                            <constraint firstItem="8Rm-YT-wZ7" firstAttribute="top" secondItem="aHp-jP-RgJ" secondAttribute="bottom" constant="20" id="u62-4u-etC"/>
                            <constraint firstItem="8Rm-YT-wZ7" firstAttribute="height" secondItem="T5I-DX-agA" secondAttribute="height" id="vid-B6-03f"/>
                            <constraint firstItem="8Rm-YT-wZ7" firstAttribute="width" secondItem="T5I-DX-agA" secondAttribute="width" id="w0d-EC-Erp"/>
                            <constraint firstItem="7Cn-GC-UIA" firstAttribute="top" secondItem="qIt-ah-XBl" secondAttribute="top" id="wy5-7A-SfF"/>
                            <constraint firstItem="Wxo-Rh-KVV" firstAttribute="top" secondItem="t2m-9Y-3Gr" secondAttribute="bottom" constant="20" id="x0A-hz-o8i"/>
                            <constraint firstItem="Its-1I-c8f" firstAttribute="leading" secondItem="T5I-DX-agA" secondAttribute="leading" id="xwQ-c1-xDh"/>
                        </constraints>
                        <variation key="default">
                            <mask key="constraints">
                                <exclude reference="vid-B6-03f"/>
                                <exclude reference="w0d-EC-Erp"/>
                            </mask>
                        </variation>
                    </view>
                    <connections>
                        <outlet property="buttonHeightConstant" destination="g0F-rb-AVX" id="7fh-Sc-ymq"/>
                        <outlet property="buttonWidthConstraint" destination="emB-rW-56o" id="Lil-Fv-rOV"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="bSl-KE-cTH" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="3" y="179"/>
        </scene>
    </scenes>
</document>
相关问题