├── .gitignore ├── LICENSE.md ├── README.md ├── StackView Demo ├── StackView Demo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata ├── StackView Demo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Base.lproj │ │ └── MainMenu.xib │ ├── Images.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── StackView Demo-Info.plist │ ├── StackView Demo-Prefix.pch │ ├── TSTextView.h │ ├── TSTextView.m │ ├── TSViewController.h │ ├── TSViewController.m │ ├── TSViewController.xib │ ├── en.lproj │ │ ├── Credits.rtf │ │ └── InfoPlist.strings │ └── main.m └── StackView DemoTests │ ├── StackView DemoTests-Info.plist │ ├── StackView_DemoTests.m │ └── en.lproj │ └── InfoPlist.strings ├── TSClipView.h ├── TSClipView.m ├── TSStackView.h └── TSStackView.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 3 | */build/* 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | profile 14 | *.moved-aside 15 | DerivedData 16 | .idea/ 17 | *.hmap 18 | *.xccheckout 19 | 20 | #CocoaPods 21 | Pods 22 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TSStackView 2 | ============ 3 | 4 | `TSStackView` is an `NSStackView` subclass that observes the `-hidden` property of its subviews to determine if a given subview should be included in the stack layout. 5 | 6 | The subclass retains all views passed to `setViews:inGravity` and performs layout as required when subview visibility changes are observed. This behaviour mimics the behaviour of the WPF `StackPanel`. 7 | 8 | The `-scrollViewContainer` property can be used to create and retrieve a `NSScrollView` instance that wraps the stack view. 9 | 10 | A simple demo project illustrates a stack of vertically expanding `NSTextViews` embedded in an `NSScrollView`. 11 | 12 | Usage 13 | ===== 14 | 15 | - (void)awakeFromNib 16 | { 17 | // add subviews 18 | [self.stackView setViews:@[self.headerView] inGravity:NSStackViewGravityTop]; 19 | [self.stackView setViews:@[self.childView1, self.childView2, self.childView3] inGravity:NSStackViewGravityCenter]; 20 | 21 | // we want our views arranged from top to bottom 22 | self.stackView.orientation = NSUserInterfaceLayoutOrientationVertical; 23 | 24 | // the internal views should be aligned with their centers 25 | self.stackView.alignment = NSLayoutAttributeCenterX; 26 | 27 | self.stackView.spacing = 0; // No spacing between the views 28 | 29 | // have the stackView strongly hug the sides of the views it contains 30 | [self.stackView setHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationHorizontal]; 31 | 32 | // have the stackView grow and shrink as its internal views grow, are added, or are removed 33 | [self.stackView setHuggingPriority:NSLayoutPriorityDefaultHigh forOrientation:NSLayoutConstraintOrientationVertical]; 34 | 35 | // toggle subview view hidden property 36 | [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(foo) userInfo:nil repeats:YES]; 37 | 38 | // wrap the stack in a scroll view and ... 39 | NSScrollView *scrollView = [stackView scrollViewContainer]; 40 | } 41 | 42 | - (void)foo 43 | { 44 | // Suspending layout only of benefit when mutating several subviews 45 | [self.stackView suspendAutoLayoutWhenSubviewVisibilityChanges]; 46 | self.childView1.hidden = !self.childView1.isHidden; 47 | self.childView2.hidden = !self.childView2.isHidden; 48 | [self.stackView resumeAutoLayoutWhenSubviewVisibilityChanges]; 49 | } 50 | 51 | Auto Content Size Options 52 | ============================== 53 | 54 | A TSStackView instance can optionally resize to match its content. This is useful when displaying an expanding list that should not clip e.g: a list of optional email addresses. Both height and width auto content sizes are supported. 55 | 56 | // StackView will resize 57 | self.stackView.autoContentSizeOptions = TSAutoContentSizeHeight; 58 | 59 | Intrinsic Content Size Options 60 | ============================== 61 | 62 | This is implemented but -autoContentSizeOptions should be preferred. 63 | 64 | NSStackView has no intrinsic content size. A TSStackView instance can optionally report an intrinsic content size equal to the unclipped content size of all visible views. 65 | 66 | // StackView will report an intrinsic height equal to the combined height of all the contained views 67 | // plus the edge insets and view spacings (default or custom). 68 | self.stackView.intrinsicContentSizeOptions = TSIntrinsicContentSizeHeight; 69 | 70 | Build requirements 71 | ================== 72 | 73 | OS X 10.9 64bit ARC 74 | 75 | Licence 76 | ======= 77 | 78 | MIT 79 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | AB4EA0A019F0677C00FD4B60 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB4EA09F19F0677C00FD4B60 /* Cocoa.framework */; }; 11 | AB4EA0AA19F0677C00FD4B60 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = AB4EA0A819F0677C00FD4B60 /* InfoPlist.strings */; }; 12 | AB4EA0AC19F0677C00FD4B60 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = AB4EA0AB19F0677C00FD4B60 /* main.m */; }; 13 | AB4EA0B019F0677C00FD4B60 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = AB4EA0AE19F0677C00FD4B60 /* Credits.rtf */; }; 14 | AB4EA0B319F0677C00FD4B60 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = AB4EA0B219F0677C00FD4B60 /* AppDelegate.m */; }; 15 | AB4EA0B619F0677C00FD4B60 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = AB4EA0B419F0677C00FD4B60 /* MainMenu.xib */; }; 16 | AB4EA0B819F0677C00FD4B60 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AB4EA0B719F0677C00FD4B60 /* Images.xcassets */; }; 17 | AB4EA0BF19F0677D00FD4B60 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB4EA0BE19F0677D00FD4B60 /* XCTest.framework */; }; 18 | AB4EA0C019F0677D00FD4B60 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB4EA09F19F0677C00FD4B60 /* Cocoa.framework */; }; 19 | AB4EA0C819F0677D00FD4B60 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = AB4EA0C619F0677D00FD4B60 /* InfoPlist.strings */; }; 20 | AB4EA0CA19F0677D00FD4B60 /* StackView_DemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB4EA0C919F0677D00FD4B60 /* StackView_DemoTests.m */; }; 21 | AB4EA0D719F0679B00FD4B60 /* TSClipView.m in Sources */ = {isa = PBXBuildFile; fileRef = AB4EA0D419F0679B00FD4B60 /* TSClipView.m */; }; 22 | AB4EA0D819F0679B00FD4B60 /* TSStackView.m in Sources */ = {isa = PBXBuildFile; fileRef = AB4EA0D619F0679B00FD4B60 /* TSStackView.m */; }; 23 | AB4EA0DC19F067F200FD4B60 /* TSViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AB4EA0DA19F067F200FD4B60 /* TSViewController.m */; }; 24 | AB4EA0DD19F067F200FD4B60 /* TSViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AB4EA0DB19F067F200FD4B60 /* TSViewController.xib */; }; 25 | AB4EA0E019F0694000FD4B60 /* TSTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = AB4EA0DF19F0694000FD4B60 /* TSTextView.m */; }; 26 | /* End PBXBuildFile section */ 27 | 28 | /* Begin PBXContainerItemProxy section */ 29 | AB4EA0C119F0677D00FD4B60 /* PBXContainerItemProxy */ = { 30 | isa = PBXContainerItemProxy; 31 | containerPortal = AB4EA09419F0677C00FD4B60 /* Project object */; 32 | proxyType = 1; 33 | remoteGlobalIDString = AB4EA09B19F0677C00FD4B60; 34 | remoteInfo = "StackView Demo"; 35 | }; 36 | /* End PBXContainerItemProxy section */ 37 | 38 | /* Begin PBXFileReference section */ 39 | AB4EA09C19F0677C00FD4B60 /* StackView Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "StackView Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 40 | AB4EA09F19F0677C00FD4B60 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 41 | AB4EA0A219F0677C00FD4B60 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; 42 | AB4EA0A319F0677C00FD4B60 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; 43 | AB4EA0A419F0677C00FD4B60 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 44 | AB4EA0A719F0677C00FD4B60 /* StackView Demo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "StackView Demo-Info.plist"; sourceTree = ""; }; 45 | AB4EA0A919F0677C00FD4B60 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 46 | AB4EA0AB19F0677C00FD4B60 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 47 | AB4EA0AD19F0677C00FD4B60 /* StackView Demo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "StackView Demo-Prefix.pch"; sourceTree = ""; }; 48 | AB4EA0AF19F0677C00FD4B60 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = en; path = en.lproj/Credits.rtf; sourceTree = ""; }; 49 | AB4EA0B119F0677C00FD4B60 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 50 | AB4EA0B219F0677C00FD4B60 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 51 | AB4EA0B519F0677C00FD4B60 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; 52 | AB4EA0B719F0677C00FD4B60 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 53 | AB4EA0BD19F0677D00FD4B60 /* StackView DemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "StackView DemoTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 54 | AB4EA0BE19F0677D00FD4B60 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; 55 | AB4EA0C519F0677D00FD4B60 /* StackView DemoTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "StackView DemoTests-Info.plist"; sourceTree = ""; }; 56 | AB4EA0C719F0677D00FD4B60 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 57 | AB4EA0C919F0677D00FD4B60 /* StackView_DemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = StackView_DemoTests.m; sourceTree = ""; }; 58 | AB4EA0D319F0679B00FD4B60 /* TSClipView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TSClipView.h; path = ../../TSClipView.h; sourceTree = ""; }; 59 | AB4EA0D419F0679B00FD4B60 /* TSClipView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSClipView.m; path = ../../TSClipView.m; sourceTree = ""; }; 60 | AB4EA0D519F0679B00FD4B60 /* TSStackView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TSStackView.h; path = ../../TSStackView.h; sourceTree = ""; }; 61 | AB4EA0D619F0679B00FD4B60 /* TSStackView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TSStackView.m; path = ../../TSStackView.m; sourceTree = ""; }; 62 | AB4EA0D919F067F200FD4B60 /* TSViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSViewController.h; sourceTree = ""; }; 63 | AB4EA0DA19F067F200FD4B60 /* TSViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TSViewController.m; sourceTree = ""; }; 64 | AB4EA0DB19F067F200FD4B60 /* TSViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TSViewController.xib; sourceTree = ""; }; 65 | AB4EA0DE19F0694000FD4B60 /* TSTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSTextView.h; sourceTree = ""; }; 66 | AB4EA0DF19F0694000FD4B60 /* TSTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TSTextView.m; sourceTree = ""; }; 67 | /* End PBXFileReference section */ 68 | 69 | /* Begin PBXFrameworksBuildPhase section */ 70 | AB4EA09919F0677C00FD4B60 /* Frameworks */ = { 71 | isa = PBXFrameworksBuildPhase; 72 | buildActionMask = 2147483647; 73 | files = ( 74 | AB4EA0A019F0677C00FD4B60 /* Cocoa.framework in Frameworks */, 75 | ); 76 | runOnlyForDeploymentPostprocessing = 0; 77 | }; 78 | AB4EA0BA19F0677D00FD4B60 /* Frameworks */ = { 79 | isa = PBXFrameworksBuildPhase; 80 | buildActionMask = 2147483647; 81 | files = ( 82 | AB4EA0C019F0677D00FD4B60 /* Cocoa.framework in Frameworks */, 83 | AB4EA0BF19F0677D00FD4B60 /* XCTest.framework in Frameworks */, 84 | ); 85 | runOnlyForDeploymentPostprocessing = 0; 86 | }; 87 | /* End PBXFrameworksBuildPhase section */ 88 | 89 | /* Begin PBXGroup section */ 90 | AB4EA09319F0677C00FD4B60 = { 91 | isa = PBXGroup; 92 | children = ( 93 | AB4EA0A519F0677C00FD4B60 /* StackView Demo */, 94 | AB4EA0C319F0677D00FD4B60 /* StackView DemoTests */, 95 | AB4EA09E19F0677C00FD4B60 /* Frameworks */, 96 | AB4EA09D19F0677C00FD4B60 /* Products */, 97 | ); 98 | sourceTree = ""; 99 | }; 100 | AB4EA09D19F0677C00FD4B60 /* Products */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | AB4EA09C19F0677C00FD4B60 /* StackView Demo.app */, 104 | AB4EA0BD19F0677D00FD4B60 /* StackView DemoTests.xctest */, 105 | ); 106 | name = Products; 107 | sourceTree = ""; 108 | }; 109 | AB4EA09E19F0677C00FD4B60 /* Frameworks */ = { 110 | isa = PBXGroup; 111 | children = ( 112 | AB4EA09F19F0677C00FD4B60 /* Cocoa.framework */, 113 | AB4EA0BE19F0677D00FD4B60 /* XCTest.framework */, 114 | AB4EA0A119F0677C00FD4B60 /* Other Frameworks */, 115 | ); 116 | name = Frameworks; 117 | sourceTree = ""; 118 | }; 119 | AB4EA0A119F0677C00FD4B60 /* Other Frameworks */ = { 120 | isa = PBXGroup; 121 | children = ( 122 | AB4EA0A219F0677C00FD4B60 /* AppKit.framework */, 123 | AB4EA0A319F0677C00FD4B60 /* CoreData.framework */, 124 | AB4EA0A419F0677C00FD4B60 /* Foundation.framework */, 125 | ); 126 | name = "Other Frameworks"; 127 | sourceTree = ""; 128 | }; 129 | AB4EA0A519F0677C00FD4B60 /* StackView Demo */ = { 130 | isa = PBXGroup; 131 | children = ( 132 | AB4EA0D319F0679B00FD4B60 /* TSClipView.h */, 133 | AB4EA0D419F0679B00FD4B60 /* TSClipView.m */, 134 | AB4EA0D519F0679B00FD4B60 /* TSStackView.h */, 135 | AB4EA0D619F0679B00FD4B60 /* TSStackView.m */, 136 | AB4EA0B119F0677C00FD4B60 /* AppDelegate.h */, 137 | AB4EA0B219F0677C00FD4B60 /* AppDelegate.m */, 138 | AB4EA0B419F0677C00FD4B60 /* MainMenu.xib */, 139 | AB4EA0B719F0677C00FD4B60 /* Images.xcassets */, 140 | AB4EA0A619F0677C00FD4B60 /* Supporting Files */, 141 | AB4EA0D919F067F200FD4B60 /* TSViewController.h */, 142 | AB4EA0DA19F067F200FD4B60 /* TSViewController.m */, 143 | AB4EA0DB19F067F200FD4B60 /* TSViewController.xib */, 144 | AB4EA0DE19F0694000FD4B60 /* TSTextView.h */, 145 | AB4EA0DF19F0694000FD4B60 /* TSTextView.m */, 146 | ); 147 | path = "StackView Demo"; 148 | sourceTree = ""; 149 | }; 150 | AB4EA0A619F0677C00FD4B60 /* Supporting Files */ = { 151 | isa = PBXGroup; 152 | children = ( 153 | AB4EA0A719F0677C00FD4B60 /* StackView Demo-Info.plist */, 154 | AB4EA0A819F0677C00FD4B60 /* InfoPlist.strings */, 155 | AB4EA0AB19F0677C00FD4B60 /* main.m */, 156 | AB4EA0AD19F0677C00FD4B60 /* StackView Demo-Prefix.pch */, 157 | AB4EA0AE19F0677C00FD4B60 /* Credits.rtf */, 158 | ); 159 | name = "Supporting Files"; 160 | sourceTree = ""; 161 | }; 162 | AB4EA0C319F0677D00FD4B60 /* StackView DemoTests */ = { 163 | isa = PBXGroup; 164 | children = ( 165 | AB4EA0C919F0677D00FD4B60 /* StackView_DemoTests.m */, 166 | AB4EA0C419F0677D00FD4B60 /* Supporting Files */, 167 | ); 168 | path = "StackView DemoTests"; 169 | sourceTree = ""; 170 | }; 171 | AB4EA0C419F0677D00FD4B60 /* Supporting Files */ = { 172 | isa = PBXGroup; 173 | children = ( 174 | AB4EA0C519F0677D00FD4B60 /* StackView DemoTests-Info.plist */, 175 | AB4EA0C619F0677D00FD4B60 /* InfoPlist.strings */, 176 | ); 177 | name = "Supporting Files"; 178 | sourceTree = ""; 179 | }; 180 | /* End PBXGroup section */ 181 | 182 | /* Begin PBXNativeTarget section */ 183 | AB4EA09B19F0677C00FD4B60 /* StackView Demo */ = { 184 | isa = PBXNativeTarget; 185 | buildConfigurationList = AB4EA0CD19F0677D00FD4B60 /* Build configuration list for PBXNativeTarget "StackView Demo" */; 186 | buildPhases = ( 187 | AB4EA09819F0677C00FD4B60 /* Sources */, 188 | AB4EA09919F0677C00FD4B60 /* Frameworks */, 189 | AB4EA09A19F0677C00FD4B60 /* Resources */, 190 | ); 191 | buildRules = ( 192 | ); 193 | dependencies = ( 194 | ); 195 | name = "StackView Demo"; 196 | productName = "StackView Demo"; 197 | productReference = AB4EA09C19F0677C00FD4B60 /* StackView Demo.app */; 198 | productType = "com.apple.product-type.application"; 199 | }; 200 | AB4EA0BC19F0677D00FD4B60 /* StackView DemoTests */ = { 201 | isa = PBXNativeTarget; 202 | buildConfigurationList = AB4EA0D019F0677D00FD4B60 /* Build configuration list for PBXNativeTarget "StackView DemoTests" */; 203 | buildPhases = ( 204 | AB4EA0B919F0677D00FD4B60 /* Sources */, 205 | AB4EA0BA19F0677D00FD4B60 /* Frameworks */, 206 | AB4EA0BB19F0677D00FD4B60 /* Resources */, 207 | ); 208 | buildRules = ( 209 | ); 210 | dependencies = ( 211 | AB4EA0C219F0677D00FD4B60 /* PBXTargetDependency */, 212 | ); 213 | name = "StackView DemoTests"; 214 | productName = "StackView DemoTests"; 215 | productReference = AB4EA0BD19F0677D00FD4B60 /* StackView DemoTests.xctest */; 216 | productType = "com.apple.product-type.bundle.unit-test"; 217 | }; 218 | /* End PBXNativeTarget section */ 219 | 220 | /* Begin PBXProject section */ 221 | AB4EA09419F0677C00FD4B60 /* Project object */ = { 222 | isa = PBXProject; 223 | attributes = { 224 | LastUpgradeCheck = 0510; 225 | ORGANIZATIONNAME = "Thesaurus Software"; 226 | TargetAttributes = { 227 | AB4EA0BC19F0677D00FD4B60 = { 228 | TestTargetID = AB4EA09B19F0677C00FD4B60; 229 | }; 230 | }; 231 | }; 232 | buildConfigurationList = AB4EA09719F0677C00FD4B60 /* Build configuration list for PBXProject "StackView Demo" */; 233 | compatibilityVersion = "Xcode 3.2"; 234 | developmentRegion = English; 235 | hasScannedForEncodings = 0; 236 | knownRegions = ( 237 | en, 238 | Base, 239 | ); 240 | mainGroup = AB4EA09319F0677C00FD4B60; 241 | productRefGroup = AB4EA09D19F0677C00FD4B60 /* Products */; 242 | projectDirPath = ""; 243 | projectRoot = ""; 244 | targets = ( 245 | AB4EA09B19F0677C00FD4B60 /* StackView Demo */, 246 | AB4EA0BC19F0677D00FD4B60 /* StackView DemoTests */, 247 | ); 248 | }; 249 | /* End PBXProject section */ 250 | 251 | /* Begin PBXResourcesBuildPhase section */ 252 | AB4EA09A19F0677C00FD4B60 /* Resources */ = { 253 | isa = PBXResourcesBuildPhase; 254 | buildActionMask = 2147483647; 255 | files = ( 256 | AB4EA0DD19F067F200FD4B60 /* TSViewController.xib in Resources */, 257 | AB4EA0AA19F0677C00FD4B60 /* InfoPlist.strings in Resources */, 258 | AB4EA0B819F0677C00FD4B60 /* Images.xcassets in Resources */, 259 | AB4EA0B019F0677C00FD4B60 /* Credits.rtf in Resources */, 260 | AB4EA0B619F0677C00FD4B60 /* MainMenu.xib in Resources */, 261 | ); 262 | runOnlyForDeploymentPostprocessing = 0; 263 | }; 264 | AB4EA0BB19F0677D00FD4B60 /* Resources */ = { 265 | isa = PBXResourcesBuildPhase; 266 | buildActionMask = 2147483647; 267 | files = ( 268 | AB4EA0C819F0677D00FD4B60 /* InfoPlist.strings in Resources */, 269 | ); 270 | runOnlyForDeploymentPostprocessing = 0; 271 | }; 272 | /* End PBXResourcesBuildPhase section */ 273 | 274 | /* Begin PBXSourcesBuildPhase section */ 275 | AB4EA09819F0677C00FD4B60 /* Sources */ = { 276 | isa = PBXSourcesBuildPhase; 277 | buildActionMask = 2147483647; 278 | files = ( 279 | AB4EA0D819F0679B00FD4B60 /* TSStackView.m in Sources */, 280 | AB4EA0B319F0677C00FD4B60 /* AppDelegate.m in Sources */, 281 | AB4EA0E019F0694000FD4B60 /* TSTextView.m in Sources */, 282 | AB4EA0AC19F0677C00FD4B60 /* main.m in Sources */, 283 | AB4EA0D719F0679B00FD4B60 /* TSClipView.m in Sources */, 284 | AB4EA0DC19F067F200FD4B60 /* TSViewController.m in Sources */, 285 | ); 286 | runOnlyForDeploymentPostprocessing = 0; 287 | }; 288 | AB4EA0B919F0677D00FD4B60 /* Sources */ = { 289 | isa = PBXSourcesBuildPhase; 290 | buildActionMask = 2147483647; 291 | files = ( 292 | AB4EA0CA19F0677D00FD4B60 /* StackView_DemoTests.m in Sources */, 293 | ); 294 | runOnlyForDeploymentPostprocessing = 0; 295 | }; 296 | /* End PBXSourcesBuildPhase section */ 297 | 298 | /* Begin PBXTargetDependency section */ 299 | AB4EA0C219F0677D00FD4B60 /* PBXTargetDependency */ = { 300 | isa = PBXTargetDependency; 301 | target = AB4EA09B19F0677C00FD4B60 /* StackView Demo */; 302 | targetProxy = AB4EA0C119F0677D00FD4B60 /* PBXContainerItemProxy */; 303 | }; 304 | /* End PBXTargetDependency section */ 305 | 306 | /* Begin PBXVariantGroup section */ 307 | AB4EA0A819F0677C00FD4B60 /* InfoPlist.strings */ = { 308 | isa = PBXVariantGroup; 309 | children = ( 310 | AB4EA0A919F0677C00FD4B60 /* en */, 311 | ); 312 | name = InfoPlist.strings; 313 | sourceTree = ""; 314 | }; 315 | AB4EA0AE19F0677C00FD4B60 /* Credits.rtf */ = { 316 | isa = PBXVariantGroup; 317 | children = ( 318 | AB4EA0AF19F0677C00FD4B60 /* en */, 319 | ); 320 | name = Credits.rtf; 321 | sourceTree = ""; 322 | }; 323 | AB4EA0B419F0677C00FD4B60 /* MainMenu.xib */ = { 324 | isa = PBXVariantGroup; 325 | children = ( 326 | AB4EA0B519F0677C00FD4B60 /* Base */, 327 | ); 328 | name = MainMenu.xib; 329 | sourceTree = ""; 330 | }; 331 | AB4EA0C619F0677D00FD4B60 /* InfoPlist.strings */ = { 332 | isa = PBXVariantGroup; 333 | children = ( 334 | AB4EA0C719F0677D00FD4B60 /* en */, 335 | ); 336 | name = InfoPlist.strings; 337 | sourceTree = ""; 338 | }; 339 | /* End PBXVariantGroup section */ 340 | 341 | /* Begin XCBuildConfiguration section */ 342 | AB4EA0CB19F0677D00FD4B60 /* Debug */ = { 343 | isa = XCBuildConfiguration; 344 | buildSettings = { 345 | ALWAYS_SEARCH_USER_PATHS = NO; 346 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 347 | CLANG_CXX_LIBRARY = "libc++"; 348 | CLANG_ENABLE_MODULES = YES; 349 | CLANG_ENABLE_OBJC_ARC = YES; 350 | CLANG_WARN_BOOL_CONVERSION = YES; 351 | CLANG_WARN_CONSTANT_CONVERSION = YES; 352 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 353 | CLANG_WARN_EMPTY_BODY = YES; 354 | CLANG_WARN_ENUM_CONVERSION = YES; 355 | CLANG_WARN_INT_CONVERSION = YES; 356 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 357 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 358 | COPY_PHASE_STRIP = NO; 359 | GCC_C_LANGUAGE_STANDARD = gnu99; 360 | GCC_DYNAMIC_NO_PIC = NO; 361 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 362 | GCC_OPTIMIZATION_LEVEL = 0; 363 | GCC_PREPROCESSOR_DEFINITIONS = ( 364 | "DEBUG=1", 365 | "$(inherited)", 366 | ); 367 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 368 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 369 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 370 | GCC_WARN_UNDECLARED_SELECTOR = YES; 371 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 372 | GCC_WARN_UNUSED_FUNCTION = YES; 373 | GCC_WARN_UNUSED_VARIABLE = YES; 374 | MACOSX_DEPLOYMENT_TARGET = 10.9; 375 | ONLY_ACTIVE_ARCH = YES; 376 | SDKROOT = macosx; 377 | }; 378 | name = Debug; 379 | }; 380 | AB4EA0CC19F0677D00FD4B60 /* Release */ = { 381 | isa = XCBuildConfiguration; 382 | buildSettings = { 383 | ALWAYS_SEARCH_USER_PATHS = NO; 384 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 385 | CLANG_CXX_LIBRARY = "libc++"; 386 | CLANG_ENABLE_MODULES = YES; 387 | CLANG_ENABLE_OBJC_ARC = YES; 388 | CLANG_WARN_BOOL_CONVERSION = YES; 389 | CLANG_WARN_CONSTANT_CONVERSION = YES; 390 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 391 | CLANG_WARN_EMPTY_BODY = YES; 392 | CLANG_WARN_ENUM_CONVERSION = YES; 393 | CLANG_WARN_INT_CONVERSION = YES; 394 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 395 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 396 | COPY_PHASE_STRIP = YES; 397 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 398 | ENABLE_NS_ASSERTIONS = NO; 399 | GCC_C_LANGUAGE_STANDARD = gnu99; 400 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 401 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 402 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 403 | GCC_WARN_UNDECLARED_SELECTOR = YES; 404 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 405 | GCC_WARN_UNUSED_FUNCTION = YES; 406 | GCC_WARN_UNUSED_VARIABLE = YES; 407 | MACOSX_DEPLOYMENT_TARGET = 10.9; 408 | SDKROOT = macosx; 409 | }; 410 | name = Release; 411 | }; 412 | AB4EA0CE19F0677D00FD4B60 /* Debug */ = { 413 | isa = XCBuildConfiguration; 414 | buildSettings = { 415 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 416 | COMBINE_HIDPI_IMAGES = YES; 417 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 418 | GCC_PREFIX_HEADER = "StackView Demo/StackView Demo-Prefix.pch"; 419 | INFOPLIST_FILE = "StackView Demo/StackView Demo-Info.plist"; 420 | PRODUCT_NAME = "$(TARGET_NAME)"; 421 | WRAPPER_EXTENSION = app; 422 | }; 423 | name = Debug; 424 | }; 425 | AB4EA0CF19F0677D00FD4B60 /* Release */ = { 426 | isa = XCBuildConfiguration; 427 | buildSettings = { 428 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 429 | COMBINE_HIDPI_IMAGES = YES; 430 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 431 | GCC_PREFIX_HEADER = "StackView Demo/StackView Demo-Prefix.pch"; 432 | INFOPLIST_FILE = "StackView Demo/StackView Demo-Info.plist"; 433 | PRODUCT_NAME = "$(TARGET_NAME)"; 434 | WRAPPER_EXTENSION = app; 435 | }; 436 | name = Release; 437 | }; 438 | AB4EA0D119F0677D00FD4B60 /* Debug */ = { 439 | isa = XCBuildConfiguration; 440 | buildSettings = { 441 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/StackView Demo.app/Contents/MacOS/StackView Demo"; 442 | COMBINE_HIDPI_IMAGES = YES; 443 | FRAMEWORK_SEARCH_PATHS = ( 444 | "$(DEVELOPER_FRAMEWORKS_DIR)", 445 | "$(inherited)", 446 | ); 447 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 448 | GCC_PREFIX_HEADER = "StackView Demo/StackView Demo-Prefix.pch"; 449 | GCC_PREPROCESSOR_DEFINITIONS = ( 450 | "DEBUG=1", 451 | "$(inherited)", 452 | ); 453 | INFOPLIST_FILE = "StackView DemoTests/StackView DemoTests-Info.plist"; 454 | PRODUCT_NAME = "$(TARGET_NAME)"; 455 | TEST_HOST = "$(BUNDLE_LOADER)"; 456 | WRAPPER_EXTENSION = xctest; 457 | }; 458 | name = Debug; 459 | }; 460 | AB4EA0D219F0677D00FD4B60 /* Release */ = { 461 | isa = XCBuildConfiguration; 462 | buildSettings = { 463 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/StackView Demo.app/Contents/MacOS/StackView Demo"; 464 | COMBINE_HIDPI_IMAGES = YES; 465 | FRAMEWORK_SEARCH_PATHS = ( 466 | "$(DEVELOPER_FRAMEWORKS_DIR)", 467 | "$(inherited)", 468 | ); 469 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 470 | GCC_PREFIX_HEADER = "StackView Demo/StackView Demo-Prefix.pch"; 471 | INFOPLIST_FILE = "StackView DemoTests/StackView DemoTests-Info.plist"; 472 | PRODUCT_NAME = "$(TARGET_NAME)"; 473 | TEST_HOST = "$(BUNDLE_LOADER)"; 474 | WRAPPER_EXTENSION = xctest; 475 | }; 476 | name = Release; 477 | }; 478 | /* End XCBuildConfiguration section */ 479 | 480 | /* Begin XCConfigurationList section */ 481 | AB4EA09719F0677C00FD4B60 /* Build configuration list for PBXProject "StackView Demo" */ = { 482 | isa = XCConfigurationList; 483 | buildConfigurations = ( 484 | AB4EA0CB19F0677D00FD4B60 /* Debug */, 485 | AB4EA0CC19F0677D00FD4B60 /* Release */, 486 | ); 487 | defaultConfigurationIsVisible = 0; 488 | defaultConfigurationName = Release; 489 | }; 490 | AB4EA0CD19F0677D00FD4B60 /* Build configuration list for PBXNativeTarget "StackView Demo" */ = { 491 | isa = XCConfigurationList; 492 | buildConfigurations = ( 493 | AB4EA0CE19F0677D00FD4B60 /* Debug */, 494 | AB4EA0CF19F0677D00FD4B60 /* Release */, 495 | ); 496 | defaultConfigurationIsVisible = 0; 497 | }; 498 | AB4EA0D019F0677D00FD4B60 /* Build configuration list for PBXNativeTarget "StackView DemoTests" */ = { 499 | isa = XCConfigurationList; 500 | buildConfigurations = ( 501 | AB4EA0D119F0677D00FD4B60 /* Debug */, 502 | AB4EA0D219F0677D00FD4B60 /* Release */, 503 | ); 504 | defaultConfigurationIsVisible = 0; 505 | }; 506 | /* End XCConfigurationList section */ 507 | }; 508 | rootObject = AB4EA09419F0677C00FD4B60 /* Project object */; 509 | } 510 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // StackView Demo 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : NSObject 12 | 13 | @property (assign) IBOutlet NSWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // StackView Demo 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "TSStackView.h" 11 | #import "TSTextView.h" 12 | #import "TSViewController.h" 13 | 14 | @interface AppDelegate() 15 | 16 | @property (weak) IBOutlet NSView *contentView; 17 | @property (strong) TSViewController *viewController1; 18 | @property (strong) TSViewController *viewController2; 19 | @property (strong) TSViewController *viewController3; 20 | @property (strong) TSStackView *stackView; 21 | @property (strong) NSScrollView *scrollView; 22 | 23 | @end 24 | 25 | @implementation AppDelegate 26 | 27 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 28 | { 29 | self.viewController1 = [TSViewController new]; 30 | self.viewController2 = [TSViewController new]; 31 | self.viewController3 = [TSViewController new]; 32 | 33 | self.stackView = [TSStackView stackViewWithViews:@[self.viewController1.view, 34 | self.viewController1.auxiliaryView, 35 | self.viewController2.view, 36 | self.viewController2.auxiliaryView, 37 | self.viewController3.view, 38 | self.viewController3.auxiliaryView]]; 39 | self.stackView.orientation = NSUserInterfaceLayoutOrientationVertical; 40 | self.stackView.alignment = NSLayoutAttributeWidth; 41 | 42 | self.scrollView = [self.stackView scrollViewContainer]; 43 | self.scrollView.drawsBackground = NO; 44 | 45 | [self addSubview:self.scrollView edgeInsets:NSEdgeInsetsMake(0, 0, 0, 0)]; 46 | } 47 | 48 | - (void)addSubview:(NSView *)subview edgeInsets:(NSEdgeInsets)edgeInsets 49 | { 50 | // this is crucial 51 | [subview setTranslatesAutoresizingMaskIntoConstraints:NO]; 52 | 53 | // add the subview 54 | [self.contentView addSubview:subview]; 55 | 56 | // add constraints 57 | NSDictionary *views = NSDictionaryOfVariableBindings(subview); 58 | NSDictionary *metrics = @{@"top" : @(edgeInsets.top), 59 | @"right" : @(edgeInsets.right), 60 | @"bottom" : @(edgeInsets.bottom), 61 | @"left" : @(edgeInsets.left), 62 | }; 63 | 64 | // in complex hierarchies it REALLY pays off to always 65 | // configure at least one constraint with a lower priority. 66 | // this can save lots of headaches with constraint violation exceptions 67 | 68 | // if an inexplicable constraint violation occurs then the solution is generally: 69 | // 1. to decrease an equality constraint priority somewhere 70 | // 2. to change an equality constraint into a less than or equality constaint. 71 | 72 | // NOTE: the lower priorty constraint here cured a lot of problems when loading views. 73 | 74 | // snap to left and right border 75 | [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[subview]-(right@990)-|" 76 | options:0 77 | metrics:metrics 78 | views:views]]; 79 | 80 | // snap to top and bottom border 81 | [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[subview]-(bottom@990)-|" 82 | options:0 83 | metrics:metrics 84 | views:views]]; 85 | 86 | } 87 | 88 | @end 89 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/Base.lproj/MainMenu.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | Default 540 | 541 | 542 | 543 | 544 | 545 | 546 | Left to Right 547 | 548 | 549 | 550 | 551 | 552 | 553 | Right to Left 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | Default 565 | 566 | 567 | 568 | 569 | 570 | 571 | Left to Right 572 | 573 | 574 | 575 | 576 | 577 | 578 | Right to Left 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "mac", 5 | "size" : "16x16", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "mac", 10 | "size" : "16x16", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "mac", 15 | "size" : "32x32", 16 | "scale" : "1x" 17 | }, 18 | { 19 | "idiom" : "mac", 20 | "size" : "32x32", 21 | "scale" : "2x" 22 | }, 23 | { 24 | "idiom" : "mac", 25 | "size" : "128x128", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "idiom" : "mac", 30 | "size" : "128x128", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "idiom" : "mac", 35 | "size" : "256x256", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "mac", 40 | "size" : "256x256", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "mac", 45 | "size" : "512x512", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "mac", 50 | "size" : "512x512", 51 | "scale" : "2x" 52 | } 53 | ], 54 | "info" : { 55 | "version" : 1, 56 | "author" : "xcode" 57 | } 58 | } -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/StackView Demo-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | com.thesaurussoftware.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2014 Thesaurus Software. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/StackView Demo-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #ifdef __OBJC__ 8 | #import 9 | #endif 10 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/TSTextView.h: -------------------------------------------------------------------------------- 1 | // 2 | // TSTextView.h 3 | // StackView Demo 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TSTextView : NSTextView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/TSTextView.m: -------------------------------------------------------------------------------- 1 | // 2 | // TSTextView.m 3 | // StackView Demo 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import "TSTextView.h" 10 | 11 | @implementation TSTextView 12 | 13 | - (NSSize)intrinsicContentSize 14 | { 15 | NSTextContainer* textContainer = [self textContainer]; 16 | NSLayoutManager* layoutManager = [self layoutManager]; 17 | [layoutManager ensureLayoutForTextContainer: textContainer]; 18 | NSSize size = [layoutManager usedRectForTextContainer: textContainer].size; 19 | return size; 20 | } 21 | 22 | - (void) didChangeText { 23 | [super didChangeText]; 24 | [self invalidateIntrinsicContentSize]; 25 | } 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/TSViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // TSViewController.h 3 | // StackView Demo 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "TSTextView.h" 11 | 12 | @interface TSViewController : NSViewController 13 | 14 | @property (strong) IBOutlet TSTextView *textView; 15 | @property (strong) IBOutlet NSView *auxiliaryView; 16 | @end 17 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/TSViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // TSViewController.m 3 | // StackView Demo 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import "TSViewController.h" 10 | 11 | @interface TSViewController () 12 | - (IBAction)toggleView:(id)sender; 13 | @end 14 | 15 | @implementation TSViewController 16 | 17 | - (id)init 18 | { 19 | return [self initWithNibName:[self className] bundle:nil]; 20 | } 21 | 22 | - (IBAction)toggleView:(id)sender 23 | { 24 | self.view.hidden = ![self.view isHidden]; 25 | } 26 | @end 27 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/TSViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/en.lproj/Credits.rtf: -------------------------------------------------------------------------------- 1 | {\rtf0\ansi{\fonttbl\f0\fswiss Helvetica;} 2 | {\colortbl;\red255\green255\blue255;} 3 | \paperw9840\paperh8400 4 | \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural 5 | 6 | \f0\b\fs24 \cf0 Engineering: 7 | \b0 \ 8 | Some people\ 9 | \ 10 | 11 | \b Human Interface Design: 12 | \b0 \ 13 | Some other people\ 14 | \ 15 | 16 | \b Testing: 17 | \b0 \ 18 | Hopefully not nobody\ 19 | \ 20 | 21 | \b Documentation: 22 | \b0 \ 23 | Whoever\ 24 | \ 25 | 26 | \b With special thanks to: 27 | \b0 \ 28 | Mom\ 29 | } 30 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /StackView Demo/StackView Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // StackView Demo 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | int main(int argc, const char * argv[]) 12 | { 13 | return NSApplicationMain(argc, argv); 14 | } 15 | -------------------------------------------------------------------------------- /StackView Demo/StackView DemoTests/StackView DemoTests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.thesaurussoftware.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundlePackageType 14 | BNDL 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleSignature 18 | ???? 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /StackView Demo/StackView DemoTests/StackView_DemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // StackView_DemoTests.m 3 | // StackView DemoTests 4 | // 5 | // Created by Jonathan Mitchell on 16/10/2014. 6 | // Copyright (c) 2014 Thesaurus Software. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface StackView_DemoTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation StackView_DemoTests 16 | 17 | - (void)setUp 18 | { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown 24 | { 25 | // Put teardown code here. This method is called after the invocation of each test method in the class. 26 | [super tearDown]; 27 | } 28 | 29 | - (void)testExample 30 | { 31 | XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__); 32 | } 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /StackView Demo/StackView DemoTests/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /TSClipView.h: -------------------------------------------------------------------------------- 1 | // 2 | // TSClipView.h 3 | // InfoBarStackView 4 | // 5 | // Created by Jonathan Mitchell on 16/12/2013. 6 | // Copyright (c) 2013 Apple Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TSClipView : NSClipView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /TSClipView.m: -------------------------------------------------------------------------------- 1 | // 2 | // TSClipView.m 3 | // InfoBarStackView 4 | // 5 | // Created by Jonathan Mitchell on 16/12/2013. 6 | // Copyright (c) 2013 Apple Inc. All rights reserved. 7 | // 8 | 9 | #import "TSClipView.h" 10 | 11 | @implementation TSClipView 12 | 13 | - (BOOL)isFlipped 14 | { 15 | return YES; 16 | } 17 | @end 18 | -------------------------------------------------------------------------------- /TSStackView.h: -------------------------------------------------------------------------------- 1 | // 2 | // TSStackView.h 3 | // BrightPay 4 | // 5 | // Created by Jonathan Mitchell on 04/12/2013. 6 | // Copyright (c) 2013 Thesaurus Software Limited. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef NS_OPTIONS(NSUInteger, TSIntrinsicContentSize) { 12 | TSIntrinsicContentSizeNone = 0 << 0, 13 | TSIntrinsicContentSizeWidth = 1 << 0, 14 | TSIntrinsicContentSizeHeight = 1 << 1, 15 | }; 16 | 17 | typedef NS_OPTIONS(NSUInteger, TSAutoContentSize) { 18 | TSAutoContentSizeNone = 0 << 0, 19 | TSAutoContentSizeWidth = 1 << 0, 20 | TSAutoContentSizeHeight = 1 << 1, 21 | }; 22 | 23 | @interface TSStackView : NSStackView 24 | 25 | /** 26 | A block that will be called when all receiver instances are first instantiated. 27 | */ 28 | @property (class, strong) void (^awakeBlock)(TSStackView *); 29 | 30 | /** 31 | A block that will be called when all scroll view containers are first instantiated. 32 | */ 33 | @property (class, strong) void (^scrollViewContainerAwakeBlock)(NSScrollView *); 34 | 35 | /*! 36 | 37 | Set options to determine whether view automatically resizes to show all content. 38 | 39 | */ 40 | @property (assign, nonatomic) TSAutoContentSize autoContentSizeOptions; 41 | 42 | /* 43 | 44 | TSStackView works by observing NSView -hidden and swapping out hidden views. 45 | NSStackView calls -fittingSize to determine the minimum size of its subviews. 46 | If a view's width or height is not constrained to a value (say the right or bottom 47 | spacing to the superview is missing) then the fitting size will be 0 in that dimension 48 | and the view will not be displayed. 49 | 50 | Given the above, other approaches to obtaining the same behaviour could therefore include: 51 | 52 | 1. Add and remove internal constraints to cause the fittingSize for views to collapse. 53 | 2. Add and remove additional external zero dimension constraints to override the internal constraints. 54 | 55 | */ 56 | 57 | /*! 58 | 59 | Create receiver with initial views in gravity. 60 | 61 | */ 62 | + (id)stackViewWithViews:(NSArray *)views inGravity:(NSStackViewGravity)gravity; 63 | 64 | /*! 65 | 66 | Suspend auto layout triggered by change in subview visibility. 67 | 68 | Auto layout may be suspended to improve performance when modiftying the visibility of a number of subviews. 69 | 70 | Must be matched with a call to -resumeAutoLayoutWhenSubviewVisibilityChanges 71 | 72 | */ 73 | - (void)suspendAutoLayoutWhenSubviewVisibilityChanges; 74 | 75 | /*! 76 | 77 | Resume auto layout triggered by change in subview visibility. 78 | 79 | */ 80 | - (void)resumeAutoLayoutWhenSubviewVisibilityChanges; 81 | 82 | /*! 83 | 84 | Hide all views in gravity 85 | 86 | */ 87 | - (void)hideViewsInGravity:(NSStackViewGravity)gravity; 88 | 89 | /*! 90 | 91 | Show all views in gravity 92 | 93 | */ 94 | - (void)showViewsInGravity:(NSStackViewGravity)gravity; 95 | 96 | /*! 97 | 98 | add views 99 | 100 | */ 101 | 102 | - (void)addViews:(NSArray *)views inGravity:(NSStackViewGravity)gravity; 103 | 104 | /*! 105 | 106 | Show views 107 | 108 | */ 109 | - (void)showViews:(NSArray *)views; 110 | 111 | /*! 112 | 113 | Hide views 114 | 115 | */ 116 | - (void)hideViews:(NSArray *)views; 117 | 118 | 119 | /*! 120 | 121 | Scroll view container with constraints to match the stackview width to the scrollview width. 122 | 123 | */ 124 | - (NSScrollView *)scrollViewContainer; 125 | 126 | /*! 127 | 128 | Set options to determine whether view reports an intrinsic content size. 129 | 130 | */ 131 | @property (assign, nonatomic) TSIntrinsicContentSize intrinsicContentSizeOptions; 132 | 133 | /*! 134 | 135 | Background fill color. 136 | 137 | */ 138 | @property (strong, nonatomic) NSColor *backgroundColor; 139 | 140 | /*! 141 | 142 | Remove all views. 143 | 144 | */ 145 | - (void)removeAllViews; 146 | 147 | - (NSArray *)allViews; 148 | 149 | /*! 150 | 151 | Block to be called when a given bound view becomes visible. 152 | 153 | */ 154 | @property (copy) void(^onBoundViewVisible)(TSStackView *stackView, NSView * view); 155 | 156 | /*! 157 | 158 | Block to be called when a given bound view becomes hidden. 159 | 160 | */ 161 | @property (copy) void(^onBoundViewHidden)(TSStackView *stackView, NSView * view); 162 | 163 | @end 164 | -------------------------------------------------------------------------------- /TSStackView.m: -------------------------------------------------------------------------------- 1 | // 2 | // TSStackView.m 3 | // BrightPay 4 | // 5 | // Created by Jonathan Mitchell on 04/12/2013. 6 | // Copyright (c) 2013 Thesaurus Software Limited. All rights reserved. 7 | // 8 | #import "TSStackView.h" 9 | #import "TSClipView.h" 10 | 11 | #define TS_LOG_SUBTREE 12 | #undef TS_LOG_SUBTREE // comment this to log the subtree 13 | 14 | @interface NSView (TSStackView) 15 | + (void)ts_disableTranslatesAutoresizingMaskIntoConstraints:(NSArray *)views; 16 | - (void)ts_disableTranslatesAutoresizingMaskIntoConstraints:(NSArray *)views; 17 | @end 18 | 19 | char BPContextHidden; 20 | 21 | @interface TSStackView () 22 | 23 | // collections 24 | @property (strong) NSMutableDictionary *observedViews; 25 | @property (strong) NSArray *stackViewConstraints; 26 | @property (strong) NSMutableArray *pendingVisibleViews; 27 | @property (strong) NSMutableArray *pendingHiddenViews; 28 | 29 | // objects 30 | @property (strong) NSLayoutConstraint *autoContentHeightConstraint; 31 | @property (strong) NSLayoutConstraint *autoContentWidthConstraint; 32 | 33 | // primitives 34 | @property (assign) BOOL scrollViewAllocated; 35 | @property BOOL doLayout; 36 | @end 37 | 38 | @implementation TSStackView 39 | 40 | #pragma mark - 41 | #pragma mark Factory 42 | 43 | + (id)stackViewWithViews:(NSArray *)views 44 | { 45 | return [self stackViewWithViews:views inGravity:NSStackViewGravityLeading]; 46 | } 47 | 48 | + (id)stackViewWithViews:(NSArray *)views inGravity:(NSStackViewGravity)gravity 49 | { 50 | views = [self flattenViews:views]; 51 | 52 | // the super call apparently guarantees that self.translatesAutoresizingMaskIntoConstraints == NO 53 | [self ts_disableTranslatesAutoresizingMaskIntoConstraints:views]; 54 | 55 | NSArray *leadingViews = nil; 56 | if (gravity == NSStackViewGravityLeading) { 57 | leadingViews = views; 58 | views = nil; 59 | } 60 | TSStackView *stackView = [super stackViewWithViews:leadingViews]; 61 | if (views) { 62 | [stackView addViews:views inGravity:gravity]; 63 | } 64 | 65 | return stackView; 66 | } 67 | 68 | #pragma mark - 69 | #pragma mark Flat stuff 70 | 71 | + (NSArray *)flattenViews:(NSArray *)views 72 | { 73 | NSMutableArray *flatViews = [NSMutableArray arrayWithCapacity:[views count]]; 74 | for (id object in views) { 75 | if ([object isKindOfClass:[NSView class]]) { 76 | [flatViews addObject:object]; 77 | } else if ([object isKindOfClass:[NSArray class]]) { 78 | [flatViews addObjectsFromArray:[self flattenViews:object]]; 79 | } else { 80 | NSLog(@"Cannot flatten this : %@", object); 81 | } 82 | } 83 | 84 | return flatViews; 85 | } 86 | 87 | #pragma mark - 88 | #pragma mark Setup 89 | 90 | - (id)initWithFrame:(NSRect)frame 91 | { 92 | self = [super initWithFrame:frame]; 93 | if (self) { 94 | [self setup]; 95 | } 96 | return self; 97 | } 98 | 99 | - (id)initWithCoder:(NSCoder *)aCoder 100 | { 101 | self = [super initWithCoder:aCoder]; 102 | if (self) { 103 | [self setup]; 104 | } 105 | return self; 106 | } 107 | 108 | - (void)setup 109 | { 110 | _observedViews = [NSMutableDictionary new]; 111 | _doLayout = YES; 112 | _pendingVisibleViews = [NSMutableArray arrayWithCapacity:3]; 113 | _pendingHiddenViews = [NSMutableArray arrayWithCapacity:3]; 114 | self.wantsLayer = YES; 115 | 116 | if (self.class.awakeBlock) { 117 | self.class.awakeBlock(self); 118 | } 119 | self.clipsToBounds = YES; 120 | } 121 | 122 | static void(^m_awakeBlock)(TSStackView *); 123 | 124 | + (void)setAwakeBlock:(void (^)(TSStackView *))awakeBlock 125 | { 126 | m_awakeBlock = awakeBlock; 127 | } 128 | 129 | + (void (^)(TSStackView *))awakeBlock 130 | { 131 | return m_awakeBlock; 132 | } 133 | 134 | #pragma mark - 135 | #pragma mark Teardown 136 | 137 | - (void)dealloc 138 | { 139 | [self removeAllViewObservations]; 140 | } 141 | 142 | #pragma mark - 143 | #pragma mark Visible views 144 | 145 | - (void)setVisibleViews:(NSArray *)views inGravity:(NSStackViewGravity)gravity 146 | { 147 | // Extract the visible items 148 | NSMutableArray *visibleViews = [NSMutableArray arrayWithCapacity:[views count]]; 149 | for (NSView *view in views) { 150 | if (!view.isHidden) [visibleViews addObject:view]; 151 | } 152 | [super setViews:visibleViews inGravity:gravity]; 153 | 154 | // call block on views pending visibility change 155 | for (NSView *view in views) { 156 | 157 | if (!view.hidden) { 158 | if ([self.pendingVisibleViews containsObject:view]) { 159 | [self.pendingVisibleViews removeObject:view]; 160 | 161 | // bound view has become visible 162 | if (self.onBoundViewVisible) { 163 | self.onBoundViewVisible(self, view); 164 | } 165 | } 166 | } 167 | else { 168 | if ([self.pendingHiddenViews containsObject:view]) { 169 | [self.pendingHiddenViews removeObject:view]; 170 | 171 | // bound view has been hidden 172 | if (self.onBoundViewHidden) { 173 | self.onBoundViewHidden(self, view); 174 | } 175 | } 176 | } 177 | } 178 | 179 | [self invalidateContentSize]; 180 | } 181 | 182 | #pragma mark - 183 | #pragma mark Observed views 184 | 185 | - (void)setObservedViews:(NSArray *)views inGravity:(NSStackViewGravity)gravity 186 | { 187 | [self removeViewObservations:[self observedViewsInGravity:gravity]]; 188 | 189 | self.observedViews[@(gravity)] = [NSMutableArray arrayWithArray:views]; 190 | 191 | [self addViewObservations:[self observedViewsInGravity:gravity]]; 192 | } 193 | 194 | - (NSArray *)observedViewsInGravity:(NSStackViewGravity)gravity 195 | { 196 | return self.observedViews[@(gravity)]; 197 | } 198 | 199 | - (NSArray *)allViewsInGravity:(NSStackViewGravity)gravity 200 | { 201 | return self.observedViews[@(gravity)]; 202 | } 203 | 204 | - (NSArray *)allViews 205 | { 206 | NSMutableArray *allViews = [NSMutableArray arrayWithCapacity:3]; 207 | for (id i in @[@(NSStackViewGravityTop), @(NSStackViewGravityCenter), @(NSStackViewGravityBottom)]) { 208 | NSArray *views = self.observedViews[i]; 209 | if (views) { 210 | [allViews addObjectsFromArray:views]; 211 | } 212 | } 213 | 214 | return allViews; 215 | } 216 | 217 | #pragma mark - 218 | #pragma mark Visibility 219 | 220 | - (void)hideViewsInGravity:(NSStackViewGravity)gravity 221 | { 222 | [self hideViews:[self viewsInGravity:gravity]]; 223 | } 224 | 225 | - (void)showViewsInGravity:(NSStackViewGravity)gravity 226 | { 227 | [self showViews:[self viewsInGravity:gravity]]; 228 | } 229 | 230 | - (void)showViews:(NSArray *)views 231 | { 232 | for (NSView *view in views) view.hidden = NO; 233 | } 234 | 235 | - (void)hideViews:(NSArray *)views 236 | { 237 | for (NSView *view in views) view.hidden = YES; 238 | } 239 | 240 | #pragma mark - 241 | #pragma mark Gravity 242 | 243 | - (NSStackViewGravity)gravityForObservedView:(NSView *)view 244 | { 245 | for (NSStackViewGravity gravity = NSStackViewGravityTop; gravity <= NSStackViewGravityBottom; gravity++) { 246 | if ([[self observedViewsInGravity:gravity] containsObject:view]) return gravity; 247 | } 248 | return NSNotFound; 249 | } 250 | 251 | - (NSStackViewGravity)gravityForView:(NSView *)view 252 | { 253 | for (NSStackViewGravity gravity = NSStackViewGravityTop; gravity <= NSStackViewGravityBottom; gravity++) { 254 | if ([[self viewsInGravity:gravity] containsObject:view]) return gravity; 255 | } 256 | return NSNotFound; 257 | } 258 | 259 | #pragma mark - 260 | #pragma mark KVO 261 | 262 | - (void)addViewObservation:(NSView *)view 263 | { 264 | [view addObserver:self forKeyPath:@"hidden" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:&BPContextHidden]; 265 | } 266 | 267 | - (void)addViewObservations:(NSArray *)views 268 | { 269 | for (NSView *view in views) { 270 | [self addViewObservation:view]; 271 | } 272 | } 273 | 274 | - (void)removeViewObservation:(NSView *)view 275 | { 276 | [view removeObserver:self forKeyPath:@"hidden" context:&BPContextHidden]; 277 | } 278 | 279 | - (void)removeViewObservations:(NSArray *)views 280 | { 281 | for (NSView *view in views) { 282 | [self removeViewObservation:view]; 283 | } 284 | } 285 | 286 | - (void)removeAllViewObservations 287 | { 288 | [self removeViewObservations:self.observedViews[@(NSStackViewGravityTop)]]; 289 | [self removeViewObservations:self.observedViews[@(NSStackViewGravityCenter)]]; 290 | [self removeViewObservations:self.observedViews[@(NSStackViewGravityBottom)]]; 291 | } 292 | 293 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 294 | { 295 | if (context == &BPContextHidden) { 296 | 297 | NSView *view = (NSView *)object; 298 | NSAssert([view isKindOfClass:[NSView class]], @"NSView expected"); 299 | 300 | // track the views transition 301 | BOOL wasHidden = NO; 302 | BOOL isHidden = NO; 303 | if (change[NSKeyValueChangeOldKey]) { 304 | wasHidden = [(NSNumber *)change[NSKeyValueChangeOldKey] boolValue]; 305 | } 306 | if (change[NSKeyValueChangeNewKey]) { 307 | isHidden = [(NSNumber *)change[NSKeyValueChangeNewKey] boolValue]; 308 | } 309 | if (wasHidden && !isHidden) { 310 | [self.pendingVisibleViews addObject:view]; 311 | } 312 | else if (isHidden && !wasHidden) { 313 | [self.pendingHiddenViews addObject:view]; 314 | } 315 | 316 | if (self.doLayout) { 317 | 318 | // get the gravity for the observed view 319 | NSStackViewGravity gravity = [self gravityForObservedView:view]; 320 | 321 | // show all visible views in the gravity 322 | [self setVisibleViews:[self observedViewsInGravity:gravity] inGravity:gravity]; 323 | } 324 | } 325 | } 326 | 327 | #pragma mark - 328 | #pragma mark AutoLayout 329 | 330 | - (void)suspendAutoLayoutWhenSubviewVisibilityChanges 331 | { 332 | self.doLayout = NO; 333 | } 334 | 335 | - (void)resumeAutoLayoutWhenSubviewVisibilityChanges 336 | { 337 | self.doLayout = YES; 338 | for (NSStackViewGravity gravity = NSStackViewGravityTop; gravity <= NSStackViewGravityBottom; gravity++) { 339 | [self setVisibleViews:[self observedViewsInGravity:gravity] inGravity:gravity]; 340 | } 341 | } 342 | 343 | - (NSSize)intrinsicContentSize 344 | { 345 | /* 346 | 347 | This works reasonably well but it is likely better to use - autoContentSizeOptions 348 | 349 | */ 350 | 351 | CGFloat intrinsicWidth = NSViewNoInstrinsicMetric; 352 | CGFloat intrinsicHeight = NSViewNoInstrinsicMetric; 353 | 354 | // This method makes assumptions about how the internal views are laid out. 355 | // The assumptions are reasonable based on the published geometry for NSStackView. 356 | // However, future changes to NSStackView may cause this method to misbehave. 357 | 358 | // calculate intrinsic content height. 359 | if (self.intrinsicContentSizeOptions & TSIntrinsicContentSizeHeight) { 360 | intrinsicHeight = 0; 361 | intrinsicHeight += (self.edgeInsets.top + self.edgeInsets.bottom); // inset 362 | 363 | for (NSView *view in self.views) { 364 | 365 | intrinsicHeight += [view fittingSize].height; 366 | 367 | // spacing 368 | if (view != self.views.lastObject) { 369 | CGFloat viewSpacing = [self customSpacingAfterView:view]; 370 | if (viewSpacing == NSStackViewSpacingUseDefault) { 371 | viewSpacing = self.spacing; 372 | } 373 | intrinsicHeight += viewSpacing; 374 | } 375 | } 376 | } 377 | 378 | // calculate intrinsic content width 379 | if (self.intrinsicContentSizeOptions & TSIntrinsicContentSizeWidth) { 380 | intrinsicWidth = 0; 381 | intrinsicWidth += (self.edgeInsets.left + self.edgeInsets.right); // inset 382 | 383 | for (NSView *view in self.views) { 384 | 385 | intrinsicWidth += [view fittingSize].width; 386 | 387 | // spacing 388 | if (view != self.views.lastObject) { 389 | CGFloat viewSpacing = [self customSpacingAfterView:view]; 390 | if (viewSpacing == NSStackViewSpacingUseDefault) { 391 | viewSpacing = self.spacing; 392 | } 393 | intrinsicWidth += viewSpacing; 394 | } 395 | } 396 | } 397 | 398 | #ifdef TS_LOG_SUBTREE 399 | if (self.intrinsicContentSizeOptions != TSIntrinsicContentSizeNone) { 400 | NSLog(@"%@ _subtreeDescription = %@", self, [self performSelector:@selector(_subtreeDescription)]); 401 | } 402 | #endif 403 | 404 | return NSMakeSize(intrinsicWidth, intrinsicHeight); 405 | } 406 | 407 | #pragma mark - 408 | #pragma mark Auto content size 409 | 410 | - (void)updateAutoContentSizeConstraints 411 | { 412 | [self removeConstraint:self.autoContentHeightConstraint]; 413 | [self removeConstraint:self.autoContentWidthConstraint]; 414 | 415 | NSView *lastView = [self.views lastObject]; 416 | if (!lastView) { 417 | return; 418 | } 419 | 420 | /* 421 | 422 | We constrain to self here. 423 | This generally works but problems can arise if we try and use edge insets too. 424 | 425 | */ 426 | NSView *stackViewContainer = self; 427 | 428 | /* 429 | 430 | Constrain the last subview to the bottom of the view 431 | 432 | */ 433 | if (self.autoContentSizeOptions & TSAutoContentSizeHeight) { 434 | 435 | self.autoContentHeightConstraint = [NSLayoutConstraint constraintWithItem:lastView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:stackViewContainer attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; 436 | 437 | [self addConstraint:self.autoContentHeightConstraint]; 438 | 439 | } 440 | 441 | /* 442 | 443 | Constrain the last subview to the right of the view 444 | 445 | */ 446 | if (self.autoContentSizeOptions & TSAutoContentSizeWidth) { 447 | 448 | self.autoContentWidthConstraint = [NSLayoutConstraint constraintWithItem:lastView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:stackViewContainer attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; 449 | 450 | [self addConstraint:self.autoContentWidthConstraint]; 451 | } 452 | } 453 | 454 | - (void)setIntrinsicContentSizeOptions:(TSIntrinsicContentSize)intrinsicSizeOptions 455 | { 456 | _intrinsicContentSizeOptions = intrinsicSizeOptions; 457 | [self invalidateContentSize]; 458 | } 459 | 460 | - (void)setAutoContentSizeOptions:(TSAutoContentSize)autoContentSizeOptions 461 | { 462 | _autoContentSizeOptions = autoContentSizeOptions; 463 | [self updateAutoContentSizeConstraints]; 464 | } 465 | 466 | - (void)invalidateContentSize 467 | { 468 | [self invalidateIntrinsicContentSize]; 469 | [self updateAutoContentSizeConstraints]; 470 | } 471 | 472 | #pragma mark - 473 | #pragma mark Adding and removing views 474 | 475 | - (void)setViews:(NSArray *)views inGravity:(NSStackViewGravity)gravity 476 | { 477 | [self ts_disableTranslatesAutoresizingMaskIntoConstraints:views]; 478 | [self setObservedViews:views inGravity:gravity]; 479 | [self setVisibleViews:views inGravity:gravity]; 480 | 481 | [self invalidateContentSize]; 482 | } 483 | 484 | - (void)addView:(NSView *)aView inGravity:(NSStackViewGravity)gravity 485 | { 486 | [self ts_disableTranslatesAutoresizingMaskIntoConstraints:@[aView]]; 487 | 488 | if (!aView.isHidden) { 489 | // this will call -insertView:atIndex:inGravity: 490 | [super addView:aView inGravity:gravity]; 491 | } else { 492 | [self commitView:aView atIndex:NSNotFound inGravity:gravity]; 493 | } 494 | } 495 | 496 | - (void)addViews:(NSArray *)views inGravity:(NSStackViewGravity)gravity 497 | { 498 | views = [self.class flattenViews:views]; 499 | for (NSView *view in views) { 500 | [self addView:view inGravity:gravity]; 501 | } 502 | } 503 | 504 | - (void)insertView:(NSView *)aView atIndex:(NSUInteger)index inGravity:(NSStackViewGravity)gravity 505 | { 506 | [self ts_disableTranslatesAutoresizingMaskIntoConstraints:@[aView]]; 507 | 508 | NSUInteger maxIndex = [self viewsInGravity:gravity].count; 509 | if (index > [self viewsInGravity:gravity].count) { 510 | index = maxIndex; 511 | } 512 | 513 | if (!aView.isHidden) { 514 | [super insertView:aView atIndex:index inGravity:gravity]; 515 | } 516 | 517 | [self commitView:aView atIndex:index inGravity:gravity]; 518 | } 519 | 520 | - (void)commitView:(NSView *)aView atIndex:(NSUInteger)index inGravity:(NSStackViewGravity)gravity 521 | { 522 | NSMutableArray *observedViews = self.observedViews[@(gravity)]; 523 | if (!observedViews) { 524 | self.observedViews[@(gravity)] = [NSMutableArray arrayWithCapacity:3]; 525 | observedViews = self.observedViews[@(gravity)]; 526 | } 527 | 528 | NSAssert(![observedViews containsObject:aView], @"View already observed"); 529 | 530 | // if no index specified then commit view has been added to list of observed gravity views. 531 | // in this case we simply append the view to the list of observed views. 532 | // if the last view in the list is hidden then the commit view will layout after it. 533 | if (index == NSNotFound) { 534 | [observedViews addObject:aView]; 535 | } 536 | else { 537 | // the commit view has been inserted at a specified index. 538 | // we will interpret this index relative to the visible views so 539 | // that when inserting the view into the observed views collection we must take account 540 | // of any preceding views that are currently hidden. 541 | NSInteger iVisible = -1; 542 | BOOL success = NO; 543 | for (NSUInteger i = 0; i < observedViews.count; i++) { 544 | NSView *view = observedViews[i]; 545 | if (!view.hidden) { 546 | iVisible++; 547 | } 548 | if (index == (NSUInteger)iVisible) { 549 | [observedViews insertObject:aView atIndex:i]; 550 | success = YES; 551 | break; 552 | } 553 | } 554 | if (!success) { 555 | [observedViews addObject:aView]; 556 | } 557 | } 558 | 559 | [self addViewObservation:aView]; 560 | 561 | [self invalidateContentSize]; 562 | } 563 | 564 | - (void)removeView:(NSView *)aView 565 | { 566 | BOOL viewRemoved = NO; 567 | 568 | for (NSUInteger gravity = NSStackViewGravityTop; gravity <= NSStackViewGravityBottom; gravity++) { 569 | 570 | NSMutableArray *observedViews = self.observedViews[@(gravity)]; 571 | if ([observedViews containsObject:aView]) { 572 | [self removeViewObservation:aView]; 573 | if (!aView.hidden) { 574 | [super removeView:aView]; 575 | } 576 | [observedViews removeObject:aView]; 577 | 578 | viewRemoved = YES; 579 | 580 | break; 581 | } 582 | 583 | } 584 | 585 | if (!viewRemoved) { 586 | NSLog(@"View not found in stack."); 587 | } 588 | 589 | [self invalidateContentSize]; 590 | } 591 | 592 | - (void)removeViews:(NSArray *)views 593 | { 594 | for (NSView *view in views) { 595 | [self removeView:view]; 596 | } 597 | } 598 | 599 | - (void)removeAllViews 600 | { 601 | for (NSUInteger gravity = NSStackViewGravityTop; gravity <= NSStackViewGravityBottom; gravity++) { 602 | NSArray *views = [self.observedViews[@(gravity)] copy]; 603 | [self removeViews:views]; 604 | 605 | NSAssert([(NSArray *)self.observedViews[@(gravity)] count] == 0, @"observed views should be 0"); 606 | } 607 | } 608 | 609 | #pragma mark - 610 | #pragma mark Embedding 611 | 612 | static void(^m_scrollViewContainerAwakeBlock)(NSScrollView *); 613 | 614 | + (void)setScrollViewContainerAwakeBlock:(void (^)(NSScrollView *))scrollViewContainerAwakeBlock 615 | { 616 | m_scrollViewContainerAwakeBlock = scrollViewContainerAwakeBlock; 617 | } 618 | 619 | + (void (^)(NSScrollView *))scrollViewContainerAwakeBlock 620 | { 621 | return m_scrollViewContainerAwakeBlock; 622 | } 623 | 624 | - (NSScrollView *)scrollViewContainer 625 | { 626 | NSScrollView *scrollView = nil; 627 | 628 | if (self.scrollViewAllocated) { 629 | scrollView = [self enclosingScrollView]; 630 | } 631 | else { 632 | self.scrollViewAllocated = YES; 633 | 634 | // allocate scroll view 635 | scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; 636 | scrollView.translatesAutoresizingMaskIntoConstraints = NO; 637 | 638 | // allocate flipped clip view 639 | TSClipView *clipView = [[TSClipView alloc] initWithFrame:scrollView.contentView.frame]; 640 | clipView.clipsToBounds = YES; 641 | scrollView.contentView = clipView; 642 | NSAssert(scrollView.contentView.isFlipped, @"ScrollView contenView must be flipped? Use TSClipView"); 643 | 644 | // configure the scrollview 645 | scrollView.borderType = NSNoBorder; 646 | scrollView.hasHorizontalScroller = YES; 647 | scrollView.hasVerticalScroller = YES; 648 | scrollView.autohidesScrollers = YES; // has no effect when scrollView.scrollerStyle == NSScrollerStyleOverlay 649 | 650 | // stackview is the document 651 | scrollView.documentView = self; 652 | scrollView.clipsToBounds = YES; 653 | 654 | // constrain stackview to match dimension of scrollview 655 | NSDictionary *viewsDict = NSDictionaryOfVariableBindings(self); 656 | NSString *vfl = nil; 657 | if (self.orientation == NSUserInterfaceLayoutOrientationVertical) { 658 | vfl = @"H:|-0-[self]-0-|"; 659 | } else { 660 | vfl = @"V:|-0-[self]-0-|"; 661 | } 662 | self.stackViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:0 metrics:nil views:viewsDict]; 663 | 664 | [scrollView addConstraints:self.stackViewConstraints]; 665 | 666 | if (self.class.scrollViewContainerAwakeBlock) { 667 | self.class.scrollViewContainerAwakeBlock(scrollView); 668 | } 669 | } 670 | 671 | return scrollView; 672 | } 673 | 674 | #pragma mark - 675 | #pragma mark Layer drawing 676 | 677 | - (BOOL)wantsUpdateLayer 678 | { 679 | return YES; 680 | } 681 | 682 | - (void)updateLayer 683 | { 684 | if (self.backgroundColor) { 685 | self.layer.backgroundColor = self.backgroundColor.CGColor; 686 | } 687 | } 688 | 689 | @end 690 | 691 | @implementation NSView (TSStackView) 692 | 693 | + (void)ts_disableTranslatesAutoresizingMaskIntoConstraints:(NSArray *)views 694 | { 695 | for (NSView *view in views) { 696 | view.translatesAutoresizingMaskIntoConstraints = NO; 697 | } 698 | } 699 | 700 | - (void)ts_disableTranslatesAutoresizingMaskIntoConstraints:(NSArray *)views 701 | { 702 | [[self class] ts_disableTranslatesAutoresizingMaskIntoConstraints:views]; 703 | } 704 | @end 705 | --------------------------------------------------------------------------------