Help us understand the problem. What is going on with this article?

AutoLayoutの制約の対象を置き換える

More than 3 years have passed since last update.

「UIImageViewとUIButtonを間違えた!」って今日後輩に言われて。
InterfaceBuilderで効率よくUIImageViewとUIbuttonを置き換える方法を調べてみた。
普通に考えてSourceCodeの方からやればできるんだろうなーってソース見たら普通に出来た。

InterFaceBuilderで「Constraints」の付け替えはできないけど、SourceCodeなら普通にできました。
スクリーンショット 2015-09-19 1.20.06.png
↑の画像みたいにImageViewと置き換えたいボタンを同じ階層配置。

Main.storyboard
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <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="fYR-Qg-Z4Q">
                                <rect key="frame" x="150" y="150" width="300" height="300"/>
                                <constraints>
                                    <constraint firstAttribute="height" constant="300" id="90K-cc-DJX"/>
                                    <constraint firstAttribute="width" constant="300" id="DIf-5E-xyL"/>
                                </constraints>
                            </imageView>
                            <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="pSR-YY-ptD">
                                <rect key="frame" x="456" y="494" width="46" height="30"/>
                                <state key="normal" title="Button">
                                    <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                </state>
                            </button>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                        <constraints>
                            <constraint firstAttribute="centerY" secondItem="fYR-Qg-Z4Q" secondAttribute="centerY" id="HUu-Jy-zPn"/>
                            <constraint firstAttribute="centerX" secondItem="fYR-Qg-Z4Q" secondAttribute="centerX" id="qRd-Cw-Lyt"/>
                        </constraints>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="332" y="-36"/>
        </scene>
    </scenes>
</document>

ッて感じになってるんですが、
重要なのが
<constraint firstAttribute="height" constant="300" id="90K-cc-DJX"/> とか「constraint」のタグが制約で。
これを見るとわけるけど90K-cc-DJXっていうのはuiimageviewのIDですね。
でこれをuibuttonのidのpSR-YY-ptDにしてあげればOK!
他の「constraints」も同じ感じでいけますね。
コード見るとあーなるほどなーで全部idで管理されてたんだーってなりました。

初投稿なので色々おかしいかもですが、誰かのお役にたてば。
間違ってらごめんなさい。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away