按钮高度无法调整

问题描述 投票:0回答:1

我有一个垂直stackview包含多个水平堆栈视图,每个视图包含一个imageview和一个按钮。我已经在垂直stackview上设置了一个限制它现在的宽度,我希望按钮断行,如果有必要,因此我将垂直stackview链接到一个名为menu的插座,并将以下代码放在我的viewcontroller中:

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

ios swift uibutton
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.