【问题标题】:Height of button doesn't adjust按钮高度没有调整
【发布时间】:2018-02-28 20:52:36
【问题描述】:

我有一个垂直堆栈视图,其中包含多个水平堆栈视图,每个堆栈视图都包含一个图像视图和一个按钮。 我在垂直stackview上设置了一个约束来固定它的宽度 现在,如果需要,我希望按钮换行,因此我将垂直堆栈视图链接到一个名为 menu 的插座,并将以下代码放入我的视图控制器中:

override func viewDidLayoutSubviews) {
    let subs = menu.subviews
    for sub in subs {
        let button = (sub as! UIStackView).subviews[1] as! UIButton
        button.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
    }
}

它可以工作,除了按钮高度不会调整,因此如果我为按钮设置背景,则文本部分位于背景之外,并与上方或下方按钮的文本重叠。我错过了什么?

这是我的故事板

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="k7d-Lf-DdI">
    <device id="ipad9_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
        <capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
        <capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
        <capability name="Named colors" minToolsVersion="9.0"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>

        <!--Main Menu View Controller-->
        <scene sceneID="xho-sd-pLC">
            <objects>
                <viewController id="k7d-Lf-DdI" customClass="MainMenuViewController" customModule="ACONEXT_Messe" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="XV4-1t-imf">
                        <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Bild1" translatesAutoresizingMaskIntoConstraints="NO" id="xDe-WB-MVY">
                                <rect key="frame" x="0.0" y="20" width="768" height="825.5"/>
                                <constraints>
                                    <constraint firstAttribute="width" secondItem="xDe-WB-MVY" secondAttribute="height" multiplier="780:838" id="gO5-cK-oIL"/>
                                </constraints>
                            </imageView>
                            <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="gAb-Td-Uze">
                                <rect key="frame" x="388" y="248" width="350" height="528"/>
                                <subviews>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ZN1-hX-aTU">
                                        <rect key="frame" x="0.0" y="0.0" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="klr-J5-Gwl">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="M0z-rb-D7a">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8KN-i0-y1t">
                                        <rect key="frame" x="0.0" y="48" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="AJh-Xg-4aR">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XQt-BQ-Wjs">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Z1F-tk-lEr">
                                        <rect key="frame" x="0.0" y="96" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="1mi-Mg-iZe">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SId-mO-ZuF">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="kjk-aA-ps2">
                                        <rect key="frame" x="0.0" y="144" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="vLW-Jr-Qu6">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="5vX-Ay-OBz">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="hSl-PB-uUB">
                                        <rect key="frame" x="0.0" y="192" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="GAP-AZ-6cQ">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="K4S-kp-XzA">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Z7X-hV-fRA">
                                        <rect key="frame" x="0.0" y="240" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="Msw-Xl-yXL">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="hiS-tx-7tX">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="3QD-df-LPX">
                                        <rect key="frame" x="0.0" y="288" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="Kbi-JW-PB5">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="uLj-Yf-S4d">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="k7n-mH-Vcq">
                                        <rect key="frame" x="0.0" y="336" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="2Ye-AN-YHI">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="tSL-1I-r3q">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="fVp-Bm-ZDd">
                                        <rect key="frame" x="0.0" y="384" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="DZR-7L-3aa">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rzn-fF-D48">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <color key="backgroundColor" red="1" green="0.1843137255" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="g7p-aG-xIi">
                                        <rect key="frame" x="0.0" y="432" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="hUE-I9-mdj">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Zij-HM-fWo">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                    <stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="X9r-t4-HaO">
                                        <rect key="frame" x="0.0" y="480" width="350" height="48"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="enumT" translatesAutoresizingMaskIntoConstraints="NO" id="EBB-JR-5ZH">
                                                <rect key="frame" x="0.0" y="0.0" width="42" height="48"/>
                                            </imageView>
                                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Wst-YV-9xu">
                                                <rect key="frame" x="42" y="0.0" width="308" height="48"/>
                                                <fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
                                                <state key="normal" title="some text">
                                                    <color key="titleColor" name="BackGroundColor"/>
                                                </state>
                                            </button>
                                        </subviews>
                                    </stackView>
                                </subviews>
                                <constraints>
                                    <constraint firstAttribute="width" constant="350" id="54x-qf-5f6"/>
                                </constraints>
                            </stackView>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                        <constraints>
                            <constraint firstItem="xDe-WB-MVY" firstAttribute="top" secondItem="dSq-dG-pDQ" secondAttribute="top" id="7eR-ko-kMe"/>
                            <constraint firstItem="dSq-dG-pDQ" firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="xDe-WB-MVY" secondAttribute="trailing" id="Ciq-JE-Xnu"/>
                            <constraint firstItem="dSq-dG-pDQ" firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="xDe-WB-MVY" secondAttribute="bottom" id="EVt-RQ-2CA"/>
                            <constraint firstItem="xDe-WB-MVY" firstAttribute="leading" secondItem="dSq-dG-pDQ" secondAttribute="leading" id="I4p-vc-AYQ"/>
                            <constraint firstItem="gAb-Td-Uze" firstAttribute="centerY" secondItem="XV4-1t-imf" secondAttribute="centerY" id="PPK-aW-84x"/>
                            <constraint firstAttribute="trailing" secondItem="gAb-Td-Uze" secondAttribute="trailing" constant="30" id="cMV-ov-cBP"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="dSq-dG-pDQ"/>
                    </view>
                    <connections>
                        <outlet property="menu" destination="gAb-Td-Uze" id="0Xl-8M-JYF"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="XaN-tK-Fs5" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="848" y="15"/>
        </scene>
    </scenes>
    <resources>
        <image name="Bild1" width="780" height="838"/>
        <image name="enumT" width="42" height="29"/>
        <namedColor name="BackGroundColor">
            <color red="0.14117647059999999" green="0.1529411765" blue="0.18823529410000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
        </namedColor>
    </resources>
</document>

正如你可能注意到的,我正在使用 SWIFT

【问题讨论】:

  • button.sizeToFit()
  • 没有改变
  • 如何添加堆栈视图?它有哪些限制条件?
  • 看看你自己,我添加了故事板。垂直的有一个固定的尾随空间来superview,垂直居中并且如上所述具有固定的宽度。

标签: ios swift uibutton


【解决方案1】:

您不应将值应用于viewDidLayoutSubviews 中的视图属性,这会更改布局。这可能会导致无限循环。你应该在viewDidLoad 或你的故事板中设置它。没有理由这样做。

由于您应该在按钮的大小检查器中将按钮的垂直内容压缩阻力优先级设置为必需(1000)。这会强制 Autolayout 使用文本完全适合的按钮的高度。您还应该将 Line Break 设置为 Word Wrap,并将按钮的左右边缘限制为堆栈视图。

【讨论】:

  • 将换行设置为自动换行?这不正是我在上面的代码中所做的吗? “限制左右边缘”是什么意思?该按钮包含在一个水平的 StackView 中,因此前导和尾随约束不起作用,无论如何,它之前有一个图像。我设置了优先级。没有效果
  • @Ginso89:阅读故事板的 XML 很难理解。您应该使用适当的图像扩展您的帖子。但是,如果按钮没有机会增加到水平方向并且没有冲突,则优先级将生效。当您在情节提要中设置所有属性时,您可以看到效果,并且 Interface Builder 将为您提供有用的冲突提示。
猜你喜欢
  • 2018-11-03
  • 2014-11-15
  • 2020-07-13
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多