ios - 按钮高度无法调整

标签 ios swift uibutton

我有一个垂直堆栈 View ,其中包含多个水平堆栈 View ,每个水平堆栈 View 都包含一个 ImageView 和一个按钮。 我对垂直堆栈 View 施加了限制,固定其宽度 现在,我希望按钮在必要时换行,因此我将垂直堆栈 View 链接到名为 menu 的 socket ,并将以下代码放入我的 View Controller 中:

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
    }
}

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

这是我的 Storyboard

<?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

最佳答案

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

因为您应该在按钮的尺寸检查器中将按钮的垂直内容压缩阻力优先级设置为必需(1000)。这会强制自动布局使用文本完全适合的按钮高度。您还应该将换行设置为自动换行,并将按钮的左右边缘限制为堆栈 View 。

关于ios - 按钮高度无法调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006577/

相关文章:

ios - NEHotspotConfiguration WPA/WPA2 密码长度无效

ios - 使用图形 API 将图像发布到 Facebook 时始终获得 "(#324) Requires upload file"

swift - 使用 TabBar 时获取两个导航栏

ios - CAGradientLayer Mask 在 Swift 中添加不透明度

ios - 如何使用 swift 从 [Any] 访问 key

ios - AFNetworking 2 和后台任务

iphone - 更改 iOS 应用的产品名称后出现代码签名错误

ios - 如何以编程方式将 uibutton 设置为右对齐?

ios - 自定义 UIButton Swift 2.0

iphone - 以编程方式切换 UIButton 上的 UILabel 文本