Skip to content

Commit 66e479d

Browse files
author
Luc Dion
authored
Merge pull request #30 from mirego/fix_sizeToFit_with_margins
Fix size to fit with margins + distribute sizeToFit() extra size correctly + New doc example
2 parents 10c8f54 + 5c6fd33 commit 66e479d

File tree

13 files changed

+300
-19
lines changed

13 files changed

+300
-19
lines changed

Docs/IntroSample-padded.png

40.9 KB
Loading

PinLayout/PinLayoutImpl.swift

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -961,14 +961,14 @@ extension PinLayoutImpl {
961961
let newSize = computeSize()
962962

963963
// Compute horizontal position
964-
if let left = _left, let right = _right {
965-
// left & right is set
966-
newRect.origin.x = left + _marginLeft
967-
newRect.size.width = right - _marginRight - newRect.origin.x
968-
} else if let left = _left, let width = newSize.width {
964+
if let left = _left, let width = newSize.width {
969965
// left & width is set
970966
newRect.origin.x = left + _marginLeft
971967
newRect.size.width = width
968+
} else if let left = _left, let right = _right {
969+
// left & right is set
970+
newRect.origin.x = left + _marginLeft
971+
newRect.size.width = right - _marginRight - newRect.origin.x
972972
} else if let right = _right, let width = newSize.width {
973973
// right & width is set
974974
newRect.size.width = width
@@ -987,19 +987,19 @@ extension PinLayoutImpl {
987987
// Only hCenter is set
988988
newRect.origin.x = _hCenter - (view.frame.width / 2)
989989
} else if let width = newSize.width {
990-
// Only widht is set
990+
// Only width is set
991991
newRect.size.width = width
992992
}
993993

994994
// Compute vertical position
995-
if let top = _top, let bottom = _bottom {
996-
// top & bottom is set
997-
newRect.origin.y = top + _marginTop
998-
newRect.size.height = bottom - _marginBottom - newRect.origin.y
999-
} else if let top = _top, let height = newSize.height {
995+
if let top = _top, let height = newSize.height {
1000996
// top & height is set
1001997
newRect.origin.y = top + _marginTop
1002998
newRect.size.height = height
999+
} else if let top = _top, let bottom = _bottom {
1000+
// top & bottom is set
1001+
newRect.origin.y = top + _marginTop
1002+
newRect.size.height = bottom - _marginBottom - newRect.origin.y
10031003
} else if let bottom = _bottom, let height = newSize.height {
10041004
// bottom & height is set
10051005
newRect.size.height = height
@@ -1083,6 +1083,34 @@ extension PinLayoutImpl {
10831083

10841084
let sizeThatFits = view.sizeThatFits(fitSize)
10851085

1086+
if newWidth != nil && newWidth! != sizeThatFits.width {
1087+
let marginToDistribute = newWidth! - sizeThatFits.width
1088+
1089+
// Distribute the width change to Margins
1090+
if let _ = _left, let _ = _right {
1091+
marginLeft = (marginLeft ?? 0) + (marginToDistribute / 2)
1092+
marginRight = (marginRight ?? 0) + (marginToDistribute / 2)
1093+
} else if let _ = _left {
1094+
marginRight = (marginRight ?? 0) + marginToDistribute
1095+
} else if let _ = _right {
1096+
marginLeft = (marginLeft ?? 0) + marginToDistribute
1097+
}
1098+
}
1099+
1100+
if newHeight != nil && newHeight! != sizeThatFits.height {
1101+
let marginToDistribute = newHeight! - sizeThatFits.height
1102+
1103+
// Distribute the height change to Margins
1104+
if let _ = _top, let _ = _bottom {
1105+
marginTop = (marginTop ?? 0) + (marginToDistribute / 2)
1106+
marginBottom = (marginBottom ?? 0) + (marginToDistribute / 2)
1107+
} else if let _ = _top {
1108+
marginBottom = (marginBottom ?? 0) + marginToDistribute
1109+
} else if let _ = _bottom {
1110+
marginTop = (marginTop ?? 0) + marginToDistribute
1111+
}
1112+
}
1113+
10861114
if fitSize.width != .greatestFiniteMagnitude && sizeThatFits.width > fitSize.width {
10871115
newWidth = fitSize.width
10881116
} else {
@@ -1100,8 +1128,8 @@ extension PinLayoutImpl {
11001128
}
11011129

11021130
fileprivate func computeWidth() -> CGFloat? {
1103-
if let _ = _left, let right = _right {
1104-
return right - _marginRight
1131+
if let left = _left, let right = _right {
1132+
return right - left - _marginLeft - _marginRight
11051133
} else if let width = width {
11061134
return width
11071135
} else {
@@ -1110,8 +1138,8 @@ extension PinLayoutImpl {
11101138
}
11111139

11121140
fileprivate func computeHeight() -> CGFloat? {
1113-
if let _ = _top, let bottom = _bottom {
1114-
return bottom - _marginBottom
1141+
if let top = _top, let bottom = _bottom {
1142+
return bottom - top - _marginTop - _marginBottom
11151143
} else if let height = height {
11161144
return height
11171145
} else {

PinLayoutSample/PinLayoutSample.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
249EFE4A1E64FAFE00165E39 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 249EFE491E64FAFE00165E39 /* Assets.xcassets */; };
2121
249EFE4D1E64FAFE00165E39 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 249EFE4B1E64FAFE00165E39 /* LaunchScreen.storyboard */; };
2222
24E654821E69041B00A72A8B /* Expect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E654811E69041B00A72A8B /* Expect.swift */; };
23+
24F75B5B1EE5644E008DB567 /* IntroView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24F75B591EE5644E008DB567 /* IntroView.swift */; };
24+
24F75B5C1EE5644E008DB567 /* IntroViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24F75B5A1EE5644E008DB567 /* IntroViewController.swift */; };
2325
DE6C3D736B571B80E207DF6A /* Pods_PinLayoutSample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AAD69688AA2A3F0994F3074E /* Pods_PinLayoutSample.framework */; };
2426
DF66F9DA1E8493E000ADB8D5 /* PinScrollingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF66F9D81E8493E000ADB8D5 /* PinScrollingView.swift */; };
2527
DF66F9DB1E8493E000ADB8D5 /* PinScrollingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF66F9D91E8493E000ADB8D5 /* PinScrollingViewController.swift */; };
@@ -78,6 +80,8 @@
7880
249EFE4C1E64FAFE00165E39 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
7981
249EFE4E1E64FAFE00165E39 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8082
24E654811E69041B00A72A8B /* Expect.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Expect.swift; sourceTree = "<group>"; };
83+
24F75B591EE5644E008DB567 /* IntroView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntroView.swift; path = Intro/IntroView.swift; sourceTree = "<group>"; };
84+
24F75B5A1EE5644E008DB567 /* IntroViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntroViewController.swift; path = Intro/IntroViewController.swift; sourceTree = "<group>"; };
8185
AAD69688AA2A3F0994F3074E /* Pods_PinLayoutSample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PinLayoutSample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8286
DF66F9D81E8493E000ADB8D5 /* PinScrollingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PinScrollingView.swift; path = PinScrolling/PinScrollingView.swift; sourceTree = "<group>"; };
8387
DF66F9D91E8493E000ADB8D5 /* PinScrollingViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PinScrollingViewController.swift; path = PinScrolling/PinScrollingViewController.swift; sourceTree = "<group>"; };
@@ -128,6 +132,7 @@
128132
2439CC371E665C5E003326FB /* Tests */ = {
129133
isa = PBXGroup;
130134
children = (
135+
24F75B581EE5642C008DB567 /* Intro */,
131136
DF66F9D61E8493D400ADB8D5 /* PinScrolling */,
132137
2439CC5F1E665F66003326FB /* MultiRelativeView */,
133138
2439CC631E66606D003326FB /* RelativeView */,
@@ -218,6 +223,15 @@
218223
path = Domain;
219224
sourceTree = "<group>";
220225
};
226+
24F75B581EE5642C008DB567 /* Intro */ = {
227+
isa = PBXGroup;
228+
children = (
229+
24F75B591EE5644E008DB567 /* IntroView.swift */,
230+
24F75B5A1EE5644E008DB567 /* IntroViewController.swift */,
231+
);
232+
name = Intro;
233+
sourceTree = "<group>";
234+
};
221235
DF66F9D61E8493D400ADB8D5 /* PinScrolling */ = {
222236
isa = PBXGroup;
223237
children = (
@@ -400,10 +414,12 @@
400414
buildActionMask = 2147483647;
401415
files = (
402416
DF66F9DA1E8493E000ADB8D5 /* PinScrollingView.swift in Sources */,
417+
24F75B5C1EE5644E008DB567 /* IntroViewController.swift in Sources */,
403418
2439CC541E665C6B003326FB /* RelativeView.swift in Sources */,
404419
2439CC551E665C6B003326FB /* RelativeViewController.swift in Sources */,
405420
2439CC351E665BF6003326FB /* MenuView.swift in Sources */,
406421
2439CC4B1E665C6B003326FB /* BasicView.swift in Sources */,
422+
24F75B5B1EE5644E008DB567 /* IntroView.swift in Sources */,
407423
249EFE431E64FAFE00165E39 /* AppDelegate.swift in Sources */,
408424
2439CC361E665BF6003326FB /* MenuViewController.swift in Sources */,
409425
2439CC531E665C6B003326FB /* MultiRelativeViewController.swift in Sources */,

PinLayoutSample/PinLayoutSample/Supporting Files/Assets.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"idiom" : "iphone",
515
"size" : "29x29",
@@ -30,6 +40,16 @@
3040
"size" : "60x60",
3141
"scale" : "3x"
3242
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "20x20",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "20x20",
51+
"scale" : "2x"
52+
},
3353
{
3454
"idiom" : "ipad",
3555
"size" : "29x29",
@@ -59,6 +79,11 @@
5979
"idiom" : "ipad",
6080
"size" : "76x76",
6181
"scale" : "2x"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"size" : "83.5x83.5",
86+
"scale" : "2x"
6287
}
6388
],
6489
"info" : {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"filename" : "PinLayout-logo.png",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
22.7 KB
Loading

PinLayoutSample/PinLayoutSample/UI/Main/MenuViewController.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import UIKit
2929

3030
enum Page: Int {
31+
case intro
3132
case relativePositions
3233
case multiRelativePositions
3334
case scrollingPin
@@ -36,6 +37,7 @@ enum Page: Int {
3637

3738
var text: String {
3839
switch self {
40+
case .intro: return "PinLayout's Intro"
3941
case .relativePositions: return "Relative Positionning"
4042
case .multiRelativePositions: return "Multiple Relatives Positionning"
4143
case .scrollingPin: return "Pin to UIScrollView"
@@ -45,6 +47,7 @@ enum Page: Int {
4547

4648
var viewController: UIViewController {
4749
switch self {
50+
case .intro: return IntroViewController()
4851
case .relativePositions: return RelativeViewController()
4952
case .multiRelativePositions: return MultiRelativeViewController()
5053
case .scrollingPin: return PinScrollingViewController()
@@ -72,10 +75,10 @@ class MenuViewController: UIViewController {
7275
mainView.delegate = self
7376
}
7477

75-
override func viewDidAppear(_ animated: Bool) {
76-
super.viewDidAppear(true)
77-
//didSelect(page: .relativePositions)
78-
}
78+
// override func viewDidAppear(_ animated: Bool) {
79+
// super.viewDidAppear(true)
80+
// didSelect(page: .intro)
81+
// }
7982
}
8083

8184
// MARK: MenuViewDelegate
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// IntroView.swift
3+
// PinLayoutSample
4+
//
5+
// Created by DION, Luc (MTL) on 2017-06-05.
6+
// Copyright (c) 2017 mcswiftlayyout.mirego.com. All rights reserved.
7+
//
8+
import UIKit
9+
10+
class IntroView: UIView {
11+
12+
// var rootView: BasicView!
13+
// var aView: BasicView!
14+
// var aViewChild: BasicView!
15+
16+
// var bView: BasicView!
17+
// var bViewChild: BasicView!
18+
19+
init() {
20+
super.init(frame: .zero)
21+
22+
backgroundColor = .black
23+
24+
// rootView = BasicView(text: "", color: .white)
25+
// addSubview(rootView)
26+
27+
// aView = BasicView(text: "View A", color: UIColor.red.withAlphaComponent(0.5))
28+
// rootView.addSubview(aView)
29+
30+
// aViewChild = BasicView(text: "View A Child", color: UIColor.red.withAlphaComponent(1))
31+
// aView.addSubview(aViewChild)
32+
33+
// bView = BasicView(text: "View B", color: UIColor.blue.withAlphaComponent(0.5))
34+
// rootView.addSubview(bView)
35+
36+
// bViewChild = BasicView(text: "View B Child", color: UIColor.blue.withAlphaComponent(0.7))
37+
// bView.addSubview(bViewChild)
38+
}
39+
40+
required init?(coder aDecoder: NSCoder) {
41+
super.init(coder: aDecoder)
42+
}
43+
44+
override func layoutSubviews() {
45+
super.layoutSubviews()
46+
47+
// rootView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
48+
// aView.frame = CGRect(x: 100, y: 100, width: 200, height: 160)
49+
// aViewChild.frame = CGRect(x: 45, y: 50, width: 80, height: 80)
50+
// bView.frame = CGRect(x: 160, y: 200, width: 40, height: 40)
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// IntroViewController.swift
3+
// PinLayoutSample
4+
//
5+
// Created by DION, Luc (MTL) on 2017-06-05.
6+
// Copyright (c) 2017 mcswiftlayyout.mirego.com. All rights reserved.
7+
//
8+
import UIKit
9+
10+
class IntroViewController: UIViewController {
11+
fileprivate var mainView: IntroView {
12+
return self.view as! IntroView
13+
}
14+
15+
init() {
16+
super.init(nibName: nil, bundle: nil)
17+
}
18+
19+
required init?(coder aDecoder: NSCoder) {
20+
super.init(coder: aDecoder)
21+
}
22+
23+
override func loadView() {
24+
view = IntroView()
25+
}
26+
}

0 commit comments

Comments
 (0)