Skip to content

Instantly share code, notes, and snippets.

@samisuteria
Created July 16, 2015 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samisuteria/2b09bc1f654d0a1a147c to your computer and use it in GitHub Desktop.
Save samisuteria/2b09bc1f654d0a1a147c to your computer and use it in GitHub Desktop.
Auto constraints vs Frame Setting with 2 Buttons Example
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="Centered" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DCM-PA-2n7">
<rect key="frame" x="0.0" y="430" width="160" height="30"/>
<color key="backgroundColor" red="1" green="0.76074701497824126" blue="0.077083629598191372" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="Vsa-Bj-t8M"/>
</constraints>
<state key="normal" title="Sign In">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Nlj-mh-blN">
<rect key="frame" x="160" y="430" width="160" height="30"/>
<color key="backgroundColor" red="0.76527254342485662" green="1" blue="0.18492738676057363" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="UEX-AG-lLf"/>
</constraints>
<state key="normal" title="Register">
<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 firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="DCM-PA-2n7" secondAttribute="bottom" constant="20" id="0IM-MD-OnH"/>
<constraint firstItem="Nlj-mh-blN" firstAttribute="leading" secondItem="DCM-PA-2n7" secondAttribute="trailing" id="B80-dK-Z6b"/>
<constraint firstAttribute="width" secondItem="DCM-PA-2n7" secondAttribute="width" multiplier="2:1" id="EeP-TX-gJS"/>
<constraint firstItem="DCM-PA-2n7" firstAttribute="width" secondItem="Nlj-mh-blN" secondAttribute="width" id="ZWJ-3L-jng"/>
<constraint firstAttribute="trailing" secondItem="Nlj-mh-blN" secondAttribute="trailing" id="l9g-5s-8Ta"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="Nlj-mh-blN" secondAttribute="bottom" constant="20" id="nl0-Xg-bLY"/>
</constraints>
</view>
<simulatedScreenMetrics key="simulatedDestinationMetrics"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="298" y="466"/>
</scene>
</scenes>
</document>
import UIKit
class ViewController: UIViewController {
var signupButton = UIButton()
var registerButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
configureButtons()
}
override func viewDidLayoutSubviews() {
signupButton.frame = CGRectMake(0, 100, view.frame.width/2, 30)
registerButton.frame = CGRectMake(view.frame.width/2, 100, view.frame.width/2, 30)
}
func configureButtons() {
signupButton.setTitle("Sign Up", forState: .Normal)
signupButton.backgroundColor = .grayColor()
registerButton.setTitle("Register", forState: .Normal)
registerButton.backgroundColor = .greenColor()
view.addSubview(signupButton)
view.addSubview(registerButton)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment