├── Images ├── 1.gif ├── 2.gif ├── 3.gif └── 4.gif ├── Demo ├── Assets.xcassets │ ├── Contents.json │ └── AppIcon.appiconset │ │ └── Contents.json ├── ViewController1.h ├── ViewController5.h ├── ViewController2.h ├── ViewController3.h ├── ViewController4.h ├── AppDelegate.h ├── UIColor+Random.h ├── main.m ├── ContentViewController.h ├── UIColor+Random.m ├── Info.plist ├── Base.lproj │ └── LaunchScreen.storyboard ├── ViewController2.m ├── ViewController1.m ├── AppDelegate.m ├── ContentViewController.m ├── ViewController3.m ├── ViewController4.m └── ViewController5.m ├── DNSPageView-ObjC.xcodeproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── daniels.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist ├── Masonry ├── MASLayoutConstraint.m ├── NSLayoutConstraint+MASDebugAdditions.h ├── MASLayoutConstraint.h ├── MASCompositeConstraint.h ├── Info.plist ├── Masonry.h ├── ViewController+MASAdditions.h ├── NSArray+MASShorthandAdditions.h ├── MASViewAttribute.m ├── MASViewAttribute.h ├── ViewController+MASAdditions.m ├── MASViewConstraint.h ├── MASConstraint+Private.h ├── NSArray+MASAdditions.h ├── View+MASAdditions.h ├── View+MASShorthandAdditions.h ├── MASCompositeConstraint.m ├── MASConstraintMaker.h ├── NSLayoutConstraint+MASDebugAdditions.m ├── MASUtilities.h ├── NSArray+MASAdditions.m ├── View+MASAdditions.m ├── MASConstraint.h ├── MASConstraint.m ├── MASConstraintMaker.m └── MASViewConstraint.m ├── DNSPageView-ObjC ├── UIColor+DNSDynamic.h ├── UIColor+DNSDynamic.m ├── Info.plist ├── DNSPageCollectionViewFlowLayout.h ├── DNSPageCollectionViewFlowLayout.m ├── UIColor+DNSRGB.h ├── DNSPageView_ObjC.h ├── UIColor+DNSRGB.m ├── DNSPageStyle.m ├── DNSPageDelegate.h ├── DNSPageView.h ├── DNSPageStyle.h ├── DNSPageContentView.h ├── DNSPageViewManager.h ├── DNSPageTitleView.h ├── DNSPageView.m ├── DNSPageViewManager.m └── DNSPageContentView.m ├── DNSPageView-ObjC.podspec ├── LICENSE ├── .gitignore └── README.md /Images/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Danie1s/DNSPageView-ObjC/HEAD/Images/1.gif -------------------------------------------------------------------------------- /Images/2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Danie1s/DNSPageView-ObjC/HEAD/Images/2.gif -------------------------------------------------------------------------------- /Images/3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Danie1s/DNSPageView-ObjC/HEAD/Images/3.gif -------------------------------------------------------------------------------- /Images/4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Danie1s/DNSPageView-ObjC/HEAD/Images/4.gif -------------------------------------------------------------------------------- /Demo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /DNSPageView-ObjC.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Demo/ViewController1.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController1.h 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController1 : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /Masonry/MASLayoutConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASLayoutConstraint.h" 10 | 11 | @implementation MASLayoutConstraint 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /DNSPageView-ObjC.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Demo/ViewController5.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController5.h 3 | // Demo 4 | // 5 | // Created by Daniels on 2020/8/16. 6 | // Copyright © 2020 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface ViewController5 : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /Demo/ViewController2.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController2.h 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface ViewController2 : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /Demo/ViewController3.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController3.h 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface ViewController3 : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /Demo/ViewController4.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController4.h 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface ViewController4 : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /Demo/UIColor+Random.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+Random.h 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/26. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UIColor (Random) 14 | 15 | + (UIColor *)randomColor; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Demo/ContentViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ContentViewController.h 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/26. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface ContentViewController : UIViewController 14 | 15 | @property (nonatomic, assign) NSInteger index; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Masonry/NSLayoutConstraint+MASDebugAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * makes debug and log output of NSLayoutConstraints more readable 13 | */ 14 | @interface NSLayoutConstraint (MASDebugAdditions) 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /Demo/UIColor+Random.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+Random.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/26. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import "UIColor+Random.h" 10 | 11 | @implementation UIColor (Random) 12 | 13 | + (UIColor *)randomColor { 14 | return [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]; 15 | } 16 | 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/UIColor+DNSDynamic.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+DNSDynamic.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels on 2020/6/16. 6 | // Copyright © 2020 Daniels. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface UIColor (DNSDynamic) 14 | 15 | + (instancetype)dns_dynamicColorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor API_AVAILABLE(ios(13.0)); 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /Masonry/MASLayoutConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * When you are debugging or printing the constraints attached to a view this subclass 13 | * makes it easier to identify which constraints have been created via Masonry 14 | */ 15 | @interface MASLayoutConstraint : NSLayoutConstraint 16 | 17 | /** 18 | * a key to associate with this constraint 19 | */ 20 | @property (nonatomic, strong) id mas_key; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /Masonry/MASCompositeConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | /** 13 | * A group of MASConstraint objects 14 | */ 15 | @interface MASCompositeConstraint : MASConstraint 16 | 17 | /** 18 | * Creates a composite with a predefined array of children 19 | * 20 | * @param children child MASConstraints 21 | * 22 | * @return a composite constraint 23 | */ 24 | - (id)initWithChildren:(NSArray *)children; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/UIColor+DNSDynamic.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+DNSDynamic.m 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels on 2020/6/16. 6 | // Copyright © 2020 Daniels. All rights reserved. 7 | // 8 | 9 | #import "UIColor+DNSDynamic.h" 10 | 11 | @implementation UIColor (DNSDynamic) 12 | 13 | + (instancetype)dns_dynamicColorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor { 14 | return [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) { 15 | if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) { 16 | return lightColor; 17 | } else { 18 | return darkColor; 19 | } 20 | }]; 21 | } 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /DNSPageView-ObjC.podspec: -------------------------------------------------------------------------------- 1 | 2 | Pod::Spec.new do |s| 3 | s.name = 'DNSPageView-ObjC' 4 | s.version = '2.1.0' 5 | s.summary = 'DNSPageView-ObjC is a library for pageView' 6 | s.description = <<-DESC 7 | DNSPageView-ObjC is a library for pageView. 8 | DESC 9 | 10 | s.homepage = 'https://github.com/Danie1s/DNSPageView-ObjC' 11 | s.license = { :type => 'MIT', :file => 'LICENSE' } 12 | s.author = { 'Daniels' => '176516837@qq.com' } 13 | s.source = { :git => 'https://github.com/Danie1s/DNSPageView-ObjC.git', :tag => s.version } 14 | 15 | s.ios.deployment_target = '8.0' 16 | s.framework = 'UIKit' 17 | s.source_files = 'DNSPageView-ObjC/*.{h,m}' 18 | s.requires_arc = true 19 | 20 | end 21 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | $(MARKETING_VERSION) 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | 22 | 23 | -------------------------------------------------------------------------------- /DNSPageView-ObjC.xcodeproj/xcuserdata/daniels.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | DNSPageView-ObjC.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | DNSPageView-ObjC.xcscheme_^#shared#^_ 13 | 14 | orderHint 15 | 0 16 | 17 | Demo.xcscheme 18 | 19 | orderHint 20 | 1 21 | 22 | Demo.xcscheme_^#shared#^_ 23 | 24 | orderHint 25 | 1 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Masonry/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Masonry/Masonry.h: -------------------------------------------------------------------------------- 1 | // 2 | // Masonry.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Masonry. 12 | FOUNDATION_EXPORT double MasonryVersionNumber; 13 | 14 | //! Project version string for Masonry. 15 | FOUNDATION_EXPORT const unsigned char MasonryVersionString[]; 16 | 17 | #import "MASUtilities.h" 18 | #import "View+MASAdditions.h" 19 | #import "View+MASShorthandAdditions.h" 20 | #import "ViewController+MASAdditions.h" 21 | #import "NSArray+MASAdditions.h" 22 | #import "NSArray+MASShorthandAdditions.h" 23 | #import "MASConstraint.h" 24 | #import "MASCompositeConstraint.h" 25 | #import "MASViewAttribute.h" 26 | #import "MASViewConstraint.h" 27 | #import "MASConstraintMaker.h" 28 | #import "MASLayoutConstraint.h" 29 | #import "NSLayoutConstraint+MASDebugAdditions.h" 30 | -------------------------------------------------------------------------------- /Masonry/ViewController+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | #ifdef MAS_VIEW_CONTROLLER 14 | 15 | @interface MAS_VIEW_CONTROLLER (MASAdditions) 16 | 17 | /** 18 | * following properties return a new MASViewAttribute with appropriate UILayoutGuide and NSLayoutAttribute 19 | */ 20 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuide; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuide; 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideTop; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideBottom; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideTop; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideBottom; 26 | 27 | 28 | @end 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 Daniels <176516837@qq.com> 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /Masonry/NSArray+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand array additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface NSArray (MASShorthandAdditions) 18 | 19 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; 20 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; 21 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; 22 | 23 | @end 24 | 25 | @implementation NSArray (MASShorthandAdditions) 26 | 27 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *))block { 28 | return [self mas_makeConstraints:block]; 29 | } 30 | 31 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *))block { 32 | return [self mas_updateConstraints:block]; 33 | } 34 | 35 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *))block { 36 | return [self mas_remakeConstraints:block]; 37 | } 38 | 39 | @end 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Masonry/MASViewAttribute.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewAttribute.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | 11 | @implementation MASViewAttribute 12 | 13 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute { 14 | self = [self initWithView:view item:view layoutAttribute:layoutAttribute]; 15 | return self; 16 | } 17 | 18 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute { 19 | self = [super init]; 20 | if (!self) return nil; 21 | 22 | _view = view; 23 | _item = item; 24 | _layoutAttribute = layoutAttribute; 25 | 26 | return self; 27 | } 28 | 29 | - (BOOL)isSizeAttribute { 30 | return self.layoutAttribute == NSLayoutAttributeWidth 31 | || self.layoutAttribute == NSLayoutAttributeHeight; 32 | } 33 | 34 | - (BOOL)isEqual:(MASViewAttribute *)viewAttribute { 35 | if ([viewAttribute isKindOfClass:self.class]) { 36 | return self.view == viewAttribute.view 37 | && self.layoutAttribute == viewAttribute.layoutAttribute; 38 | } 39 | return [super isEqual:viewAttribute]; 40 | } 41 | 42 | - (NSUInteger)hash { 43 | return MAS_NSUINTROTATE([self.view hash], MAS_NSUINT_BIT / 2) ^ self.layoutAttribute; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /Masonry/MASViewAttribute.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewAttribute.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * An immutable tuple which stores the view and the related NSLayoutAttribute. 13 | * Describes part of either the left or right hand side of a constraint equation 14 | */ 15 | @interface MASViewAttribute : NSObject 16 | 17 | /** 18 | * The view which the reciever relates to. Can be nil if item is not a view. 19 | */ 20 | @property (nonatomic, weak, readonly) MAS_VIEW *view; 21 | 22 | /** 23 | * The item which the reciever relates to. 24 | */ 25 | @property (nonatomic, weak, readonly) id item; 26 | 27 | /** 28 | * The attribute which the reciever relates to 29 | */ 30 | @property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute; 31 | 32 | /** 33 | * Convenience initializer. 34 | */ 35 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute; 36 | 37 | /** 38 | * The designated initializer. 39 | */ 40 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute; 41 | 42 | /** 43 | * Determine whether the layoutAttribute is a size attribute 44 | * 45 | * @return YES if layoutAttribute is equal to NSLayoutAttributeWidth or NSLayoutAttributeHeight 46 | */ 47 | - (BOOL)isSizeAttribute; 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /Masonry/ViewController+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "ViewController+MASAdditions.h" 10 | 11 | #ifdef MAS_VIEW_CONTROLLER 12 | 13 | @implementation MAS_VIEW_CONTROLLER (MASAdditions) 14 | 15 | - (MASViewAttribute *)mas_topLayoutGuide { 16 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 17 | } 18 | - (MASViewAttribute *)mas_topLayoutGuideTop { 19 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 20 | } 21 | - (MASViewAttribute *)mas_topLayoutGuideBottom { 22 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 23 | } 24 | 25 | - (MASViewAttribute *)mas_bottomLayoutGuide { 26 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 27 | } 28 | - (MASViewAttribute *)mas_bottomLayoutGuideTop { 29 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 30 | } 31 | - (MASViewAttribute *)mas_bottomLayoutGuideBottom { 32 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 33 | } 34 | 35 | 36 | 37 | @end 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Masonry/MASViewConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | #import "MASUtilities.h" 13 | 14 | /** 15 | * A single constraint. 16 | * Contains the attributes neccessary for creating a NSLayoutConstraint and adding it to the appropriate view 17 | */ 18 | @interface MASViewConstraint : MASConstraint 19 | 20 | /** 21 | * First item/view and first attribute of the NSLayoutConstraint 22 | */ 23 | @property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute; 24 | 25 | /** 26 | * Second item/view and second attribute of the NSLayoutConstraint 27 | */ 28 | @property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute; 29 | 30 | /** 31 | * initialises the MASViewConstraint with the first part of the equation 32 | * 33 | * @param firstViewAttribute view.mas_left, view.mas_width etc. 34 | * 35 | * @return a new view constraint 36 | */ 37 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute; 38 | 39 | /** 40 | * Returns all MASViewConstraints installed with this view as a first item. 41 | * 42 | * @param view A view to retrieve constraints for. 43 | * 44 | * @return An array of MASViewConstraints. 45 | */ 46 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageCollectionViewFlowLayout.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageCollectionViewFlowLayout.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels on 2018/9/24. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | NS_ASSUME_NONNULL_BEGIN 30 | 31 | @interface DNSPageCollectionViewFlowLayout : UICollectionViewFlowLayout 32 | 33 | @property (nonatomic, assign) CGFloat offset; 34 | 35 | @end 36 | 37 | NS_ASSUME_NONNULL_END 38 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageCollectionViewFlowLayout.m: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageCollectionViewFlowLayout.m 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels on 2018/9/24. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "DNSPageCollectionViewFlowLayout.h" 28 | 29 | @implementation DNSPageCollectionViewFlowLayout 30 | 31 | - (void)prepareLayout { 32 | [super prepareLayout]; 33 | if (self.offset) { 34 | self.collectionView.contentOffset = CGPointMake(self.offset, 0); 35 | } 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleDisplayName 8 | DNSPageView-ObjC 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/UIColor+DNSRGB.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+DNSRGB.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/26. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | struct RGBColorSpace { 30 | CGFloat red; 31 | CGFloat green; 32 | CGFloat blue; 33 | }; 34 | 35 | typedef struct RGBColorSpace RGBColorSpace; 36 | 37 | NS_ASSUME_NONNULL_BEGIN 38 | 39 | @interface UIColor (RGB) 40 | 41 | - (RGBColorSpace)dns_getRGBColorSpace; 42 | 43 | + (instancetype)dns_colorWithRGBColorSpace:(RGBColorSpace)rgb; 44 | 45 | @end 46 | 47 | NS_ASSUME_NONNULL_END 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | # OSX 10 | .DS_Store 11 | 12 | ## Various settings 13 | *.pbxuser 14 | !default.pbxuser 15 | *.mode1v3 16 | !default.mode1v3 17 | *.mode2v3 18 | !default.mode2v3 19 | *.perspectivev3 20 | !default.perspectivev3 21 | xcuserdata/ 22 | 23 | ## Other 24 | *.moved-aside 25 | *.xccheckout 26 | *.xcscmblueprint 27 | 28 | ## Obj-C/Swift specific 29 | *.hmap 30 | *.ipa 31 | *.dSYM.zip 32 | *.dSYM 33 | 34 | ## Playgrounds 35 | timeline.xctimeline 36 | playground.xcworkspace 37 | 38 | # Swift Package Manager 39 | # 40 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 41 | # Packages/ 42 | # Package.pins 43 | # Package.resolved 44 | .build/ 45 | 46 | # CocoaPods 47 | # 48 | # We recommend against adding the Pods directory to your .gitignore. However 49 | # you should judge for yourself, the pros and cons are mentioned at: 50 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 51 | # 52 | Pods/ 53 | *.xcworkspace 54 | 55 | # Carthage 56 | # 57 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 58 | # Carthage/Checkouts 59 | 60 | Carthage/Build 61 | 62 | # fastlane 63 | # 64 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 65 | # screenshots whenever they are needed. 66 | # For more information about the recommended setup visit: 67 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 68 | 69 | fastlane/report.xml 70 | fastlane/Preview.html 71 | fastlane/screenshots 72 | fastlane/test_output 73 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageView_ObjC.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageView_ObjC.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | //! Project version number for DNSPageView_ObjC. 30 | FOUNDATION_EXPORT double DNSPageView_ObjCVersionNumber; 31 | 32 | //! Project version string for DNSPageView_ObjC. 33 | FOUNDATION_EXPORT const unsigned char DNSPageView_ObjCVersionString[]; 34 | 35 | #import "DNSPageStyle.h" 36 | #import "DNSPageTitleView.h" 37 | #import "DNSPageContentView.h" 38 | #import "DNSPageView.h" 39 | #import "DNSPageViewManager.h" 40 | #import "DNSPageDelegate.h" 41 | #import "UIColor+DNSDynamic.h" 42 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/UIColor+DNSRGB.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+RGB.m 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/26. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "UIColor+DNSRGB.h" 28 | 29 | @implementation UIColor (RGB) 30 | 31 | - (RGBColorSpace)dns_getRGBColorSpace { 32 | CGFloat red = 0; 33 | CGFloat green = 0; 34 | CGFloat blue = 0; 35 | [self getRed:&red green:&green blue:&blue alpha:nil]; 36 | RGBColorSpace rgb; 37 | rgb.red = red * 255; 38 | rgb.green = green * 255; 39 | rgb.blue = blue * 255; 40 | return rgb; 41 | } 42 | 43 | + (instancetype)dns_colorWithRGBColorSpace:(RGBColorSpace)rgb { 44 | return [self colorWithRed:rgb.red / 255 green:rgb.green / 255 blue:rgb.blue / 255 alpha:1.0]; 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /Demo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Demo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /Masonry/MASConstraint+Private.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint+Private.h 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 29/04/14. 6 | // Copyright (c) 2014 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | 11 | @protocol MASConstraintDelegate; 12 | 13 | 14 | @interface MASConstraint () 15 | 16 | /** 17 | * Whether or not to check for an existing constraint instead of adding constraint 18 | */ 19 | @property (nonatomic, assign) BOOL updateExisting; 20 | 21 | /** 22 | * Usually MASConstraintMaker but could be a parent MASConstraint 23 | */ 24 | @property (nonatomic, weak) id delegate; 25 | 26 | /** 27 | * Based on a provided value type, is equal to calling: 28 | * NSNumber - setOffset: 29 | * NSValue with CGPoint - setPointOffset: 30 | * NSValue with CGSize - setSizeOffset: 31 | * NSValue with MASEdgeInsets - setInsets: 32 | */ 33 | - (void)setLayoutConstantWithValue:(NSValue *)value; 34 | 35 | @end 36 | 37 | 38 | @interface MASConstraint (Abstract) 39 | 40 | /** 41 | * Sets the constraint relation to given NSLayoutRelation 42 | * returns a block which accepts one of the following: 43 | * MASViewAttribute, UIView, NSValue, NSArray 44 | * see readme for more details. 45 | */ 46 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation; 47 | 48 | /** 49 | * Override to set a custom chaining behaviour 50 | */ 51 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 52 | 53 | @end 54 | 55 | 56 | @protocol MASConstraintDelegate 57 | 58 | /** 59 | * Notifies the delegate when the constraint needs to be replaced with another constraint. For example 60 | * A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks 61 | */ 62 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint; 63 | 64 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /Demo/ViewController2.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController2.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import "ViewController2.h" 10 | #import 11 | #import "ContentViewController.h" 12 | #import "UIColor+Random.h" 13 | 14 | @interface ViewController2 () 15 | 16 | @property (weak, nonatomic) IBOutlet DNSPageTitleView *titleView; 17 | 18 | @property (weak, nonatomic) IBOutlet DNSPageContentView *contentView; 19 | 20 | @property (nonatomic, strong) DNSPageViewManager *pageViewManager; 21 | 22 | @end 23 | 24 | @implementation ViewController2 25 | 26 | - (void)viewDidLoad { 27 | [super viewDidLoad]; 28 | 29 | if (@available(iOS 11, *)) { 30 | } else { 31 | self.automaticallyAdjustsScrollViewInsets = NO; 32 | } 33 | 34 | // 创建DNSPageStyle,设置样式 35 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 36 | style.titleViewBackgroundColor = [UIColor redColor]; 37 | style.showCoverView = YES; 38 | 39 | // 设置标题内容 40 | NSArray *titles = @[@"头条", @"视频", @"娱乐", @"要问", @"体育"]; 41 | 42 | // 设置默认的起始位置 43 | NSInteger currentIndex = 2; 44 | 45 | 46 | // 创建每一页对应的 controller 47 | for (int i = 0; i < titles.count; i++) { 48 | ContentViewController *controller = [[ContentViewController alloc] init]; 49 | controller.view.backgroundColor = [UIColor randomColor]; 50 | controller.index = i; 51 | [self addChildViewController:controller]; 52 | } 53 | 54 | self.pageViewManager = [[DNSPageViewManager alloc] initWithStyle:style 55 | titles:titles 56 | childViewControllers:self.childViewControllers 57 | currentIndex:currentIndex 58 | titleView:self.titleView 59 | contentView:self.contentView]; 60 | } 61 | 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /Demo/ViewController1.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController1.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import "ViewController1.h" 10 | #import "ContentViewController.h" 11 | #import 12 | #import "UIColor+Random.h" 13 | 14 | @interface ViewController1 () 15 | 16 | @end 17 | 18 | @implementation ViewController1 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | 23 | if (@available(iOS 11, *)) { 24 | } else { 25 | self.automaticallyAdjustsScrollViewInsets = NO; 26 | } 27 | 28 | // 创建 DNSPageStyle,设置样式 29 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 30 | style.titleViewScrollEnabled = YES; 31 | style.titleMargin = 10; 32 | style.titleInset = 20; 33 | style.titleSelectedFont = [UIFont systemFontOfSize:20]; 34 | // style.titleScaleEnabled = YES; 35 | 36 | // 设置标题内容 37 | NSArray *titles = @[@"头条", @"视频", @"娱乐", @"要问", @"体育", @"科技", @"汽车", @"时尚", @"图片", @"游戏", @"房产"]; 38 | 39 | // 创建每一页对应的 controller 40 | for (int i = 0; i < titles.count; i++) { 41 | ContentViewController *controller = [[ContentViewController alloc] init]; 42 | controller.view.backgroundColor = [UIColor randomColor]; 43 | controller.index = i; 44 | [self addChildViewController:controller]; 45 | } 46 | 47 | CGFloat y = [UIApplication sharedApplication].statusBarFrame.size.height + self.navigationController.navigationBar.frame.size.height; 48 | CGSize size = [UIScreen mainScreen].bounds.size; 49 | 50 | // 创建对应的 DNSPageView,并设置它的 frame 51 | DNSPageView *pageView = [[DNSPageView alloc] initWithFrame:CGRectMake(0, y, size.width, size.height - y) 52 | style:style 53 | titles:titles 54 | childViewControllers:self.childViewControllers 55 | currentIndex:7]; 56 | [self.view addSubview:pageView]; 57 | } 58 | 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /Demo/ContentViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ContentViewController.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/26. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import "ContentViewController.h" 10 | #import 11 | 12 | #define MAS_SHORTHAND 13 | #define MAS_SHORTHAND_GLOBALS 14 | #import "Masonry.h" 15 | 16 | @interface ContentViewController () 17 | 18 | @property (nonatomic, strong) UIButton *button; 19 | 20 | @end 21 | 22 | @implementation ContentViewController 23 | 24 | - (UIButton *)button { 25 | if (!_button) { 26 | _button = [[UIButton alloc] init]; 27 | [_button setTitle:@"点击我进行push" forState:UIControlStateNormal]; 28 | [_button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside]; 29 | [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 30 | _button.backgroundColor = [UIColor whiteColor]; 31 | } 32 | return _button; 33 | } 34 | 35 | - (void)viewDidLoad { 36 | [super viewDidLoad]; 37 | 38 | [self.view addSubview:self.button]; 39 | [self.button makeConstraints:^(MASConstraintMaker *make) { 40 | make.center.equalTo(self.view); 41 | }]; 42 | } 43 | 44 | // 出现的时候马上调用 45 | - (void)viewDidAppear:(BOOL)animated { 46 | [super viewDidAppear:animated]; 47 | // NSLog(@"viewDidAppear,index: %ld", self.index); 48 | } 49 | 50 | // pop 或者 cell 复用的时候调用 51 | - (void)viewDidDisappear:(BOOL)animated { 52 | [super viewDidDisappear:animated]; 53 | // NSLog(@"viewDidDisappear,index: %ld", self.index); 54 | } 55 | 56 | 57 | - (void)push { 58 | UIViewController *controller = [[UIViewController alloc] init]; 59 | controller.view.backgroundColor = [UIColor whiteColor]; 60 | controller.hidesBottomBarWhenPushed = YES; 61 | [self.navigationController pushViewController:controller animated:YES]; 62 | } 63 | 64 | 65 | - (void)titleViewDidSelectSameTitle { 66 | NSLog(@"重复点击了标题,index: %ld", self.index); 67 | 68 | } 69 | 70 | // 当前 controller 滑动结束的时候调用 71 | - (void)contentViewDidEndScroll { 72 | NSLog(@"contentView滑动结束,index: %ld", self.index); 73 | } 74 | 75 | - (void)contentViewDidDisappear { 76 | NSLog(@"我消失了,index: %ld", self.index); 77 | } 78 | 79 | @end 80 | -------------------------------------------------------------------------------- /Demo/ViewController3.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController3.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import "ViewController3.h" 10 | #import 11 | #import "ContentViewController.h" 12 | #import "UIColor+Random.h" 13 | 14 | #define MAS_SHORTHAND 15 | #define MAS_SHORTHAND_GLOBALS 16 | #import "Masonry.h" 17 | 18 | @interface ViewController3 () 19 | 20 | @property (nonatomic, strong) DNSPageViewManager *pageViewManager; 21 | 22 | @end 23 | 24 | @implementation ViewController3 25 | 26 | - (DNSPageViewManager *)pageViewManager { 27 | if (!_pageViewManager) { 28 | // 创建 DNSPageStyle,设置样式 29 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 30 | style.showBottomLine = YES; 31 | style.titleViewScrollEnabled = YES; 32 | style.titleViewBackgroundColor = [UIColor clearColor]; 33 | 34 | // 设置标题内容 35 | NSArray *titles = @[@"头条", @"视频", @"娱乐", @"要问", @"体育"]; 36 | 37 | // 创建每一页对应的 controller 38 | for (int i = 0; i < titles.count; i++) { 39 | ContentViewController *controller = [[ContentViewController alloc] init]; 40 | controller.view.backgroundColor = [UIColor randomColor]; 41 | controller.index = i; 42 | [self addChildViewController:controller]; 43 | } 44 | _pageViewManager = [[DNSPageViewManager alloc] initWithStyle:style 45 | titles:titles 46 | childViewControllers:self.childViewControllers]; 47 | } 48 | return _pageViewManager; 49 | } 50 | 51 | - (void)viewDidLoad { 52 | [super viewDidLoad]; 53 | 54 | // 单独设置 titleView 的 frame 55 | self.navigationItem.titleView = self.pageViewManager.titleView; 56 | self.pageViewManager.titleView.frame = CGRectMake(0, 0, 180, 44); 57 | 58 | // 单独设置 contentView 的大小和位置,可以使用 autolayout 或者 frame 59 | DNSPageContentView *contentView = self.pageViewManager.contentView; 60 | [self.view addSubview:contentView]; 61 | [contentView makeConstraints:^(MASConstraintMaker *make) { 62 | make.edges.equalTo(self.view); 63 | }]; 64 | 65 | if (@available(iOS 11, *)) { 66 | contentView.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; 67 | } else { 68 | self.automaticallyAdjustsScrollViewInsets = NO; 69 | } 70 | } 71 | 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageStyle.m: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageStyle.m 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "DNSPageStyle.h" 28 | 29 | @implementation DNSPageStyle 30 | 31 | - (instancetype)init { 32 | self = [super init]; 33 | if (self) { 34 | self.titleViewHeight = 44; 35 | self.titleColor = [UIColor blackColor]; 36 | self.titleSelectedColor = [UIColor blueColor]; 37 | self.titleFont = [UIFont systemFontOfSize:15]; 38 | self.titleViewBackgroundColor = [UIColor whiteColor]; 39 | self.titleViewSelectedColor = [UIColor clearColor]; 40 | self.titleMargin = 30; 41 | self.titleInset = 0; 42 | 43 | self.titleViewScrollEnabled = NO; 44 | 45 | self.showBottomLine = NO; 46 | self.bottomLineColor = [UIColor blueColor]; 47 | self.bottomLineWidth = 0; 48 | self.bottomLineHeight = 2; 49 | self.bottomLineRadius = 1; 50 | 51 | self.titleScaleEnabled = NO; 52 | self.titleMaximumScaleFactor = 1.2; 53 | 54 | self.showCoverView = NO; 55 | self.coverViewBackgroundColor = [UIColor blackColor]; 56 | self.coverViewAlpha = 0.4; 57 | self.coverMargin = 8; 58 | self.coverViewHeight = 25; 59 | self.coverViewRadius = 12; 60 | 61 | self.contentScrollEnabled = YES; 62 | self.contentViewBackgroundColor = [UIColor whiteColor]; 63 | } 64 | return self; 65 | } 66 | 67 | @end 68 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageDelegate.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/26. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | NS_ASSUME_NONNULL_BEGIN 30 | 31 | @class DNSPageTitleView, DNSPageContentView; 32 | 33 | 34 | /// DNSPageView 的事件回调,如果有需要,请让对应的 childViewController 遵守这个协议 35 | @protocol DNSPageEventHandlerDelegate 36 | @optional 37 | 38 | 39 | 40 | /// 重复点击 pageTitleView 后调用 41 | - (void)titleViewDidSelectSameTitle; 42 | 43 | 44 | /// pageContentView 的上一页消失的时候,上一页对应的 controller 调用 45 | - (void)contentViewDidDisappear; 46 | 47 | /// pageContentView 滚动停止的时候,当前页对应的 controller 调用 48 | - (void)contentViewDidEndScroll; 49 | 50 | @end 51 | 52 | @protocol DNSPageTitleViewDelegate 53 | 54 | @property (nullable, nonatomic, weak) id eventHandler; 55 | 56 | - (void)titleView:(DNSPageTitleView *)titleView didSelectAtIndex:(NSInteger)index; 57 | 58 | @end 59 | 60 | 61 | @protocol DNSPageContentViewDelegate 62 | 63 | - (void)contentView:(DNSPageContentView *)contentView didEndScrollAtIndex:(NSInteger)index; 64 | 65 | - (void)contentView:(DNSPageContentView *)contentView 66 | scrollingWithSourceIndex:(NSInteger)sourceIndex 67 | targetIndex:(NSInteger)targetIndex 68 | progress:(CGFloat)progress; 69 | 70 | @end 71 | 72 | @protocol DNSPageViewContainer 73 | 74 | - (void)updateCurrentIndex:(NSInteger)index; 75 | 76 | @end 77 | 78 | 79 | 80 | NS_ASSUME_NONNULL_END 81 | -------------------------------------------------------------------------------- /Masonry/NSArray+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.h 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | typedef NS_ENUM(NSUInteger, MASAxisType) { 14 | MASAxisTypeHorizontal, 15 | MASAxisTypeVertical 16 | }; 17 | 18 | @interface NSArray (MASAdditions) 19 | 20 | /** 21 | * Creates a MASConstraintMaker with each view in the callee. 22 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing on each view 23 | * 24 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 25 | * 26 | * @return Array of created MASConstraints 27 | */ 28 | - (NSArray *)mas_makeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 29 | 30 | /** 31 | * Creates a MASConstraintMaker with each view in the callee. 32 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 33 | * If an existing constraint exists then it will be updated instead. 34 | * 35 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 36 | * 37 | * @return Array of created/updated MASConstraints 38 | */ 39 | - (NSArray *)mas_updateConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 40 | 41 | /** 42 | * Creates a MASConstraintMaker with each view in the callee. 43 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 44 | * All constraints previously installed for the views will be removed. 45 | * 46 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 47 | * 48 | * @return Array of created/updated MASConstraints 49 | */ 50 | - (NSArray *)mas_remakeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block; 51 | 52 | /** 53 | * distribute with fixed spacing 54 | * 55 | * @param axisType which axis to distribute items along 56 | * @param fixedSpacing the spacing between each item 57 | * @param leadSpacing the spacing before the first item and the container 58 | * @param tailSpacing the spacing after the last item and the container 59 | */ 60 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 61 | 62 | /** 63 | * distribute with fixed item size 64 | * 65 | * @param axisType which axis to distribute items along 66 | * @param fixedItemLength the fixed length of each item 67 | * @param leadSpacing the spacing before the first item and the container 68 | * @param tailSpacing the spacing after the last item and the container 69 | */ 70 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageView.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageView.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | #import "DNSPageStyle.h" 29 | #import "DNSPageDelegate.h" 30 | 31 | @class DNSPageTitleView, DNSPageContentView; 32 | NS_ASSUME_NONNULL_BEGIN 33 | 34 | /// 通过这个类创建的 pageView,默认 titleView 和 contentView 连在一起,效果类似于网易新闻 35 | /// 只能用代码创建,不能在 xib 或者 storyboard 里面使用 36 | @interface DNSPageView : UIView 37 | 38 | @property (nonatomic, strong, readonly) DNSPageStyle *style; 39 | 40 | @property (nonatomic, strong, readonly) NSArray *titles; 41 | 42 | @property (nonatomic, strong, readonly) NSArray *childViewControllers; 43 | 44 | @property (nonatomic, assign, readonly) NSInteger currentIndex; 45 | 46 | @property (nonatomic, strong, readonly) DNSPageTitleView *titleView; 47 | 48 | @property (nonatomic, strong, readonly) DNSPageContentView *contentView; 49 | 50 | - (instancetype)init NS_UNAVAILABLE; 51 | 52 | - (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; 53 | 54 | - (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE; 55 | 56 | - (instancetype)initWithFrame:(CGRect)frame 57 | style:(DNSPageStyle *)style 58 | titles:(NSArray *)titles 59 | childViewControllers:(NSArray *)childViewControllers 60 | currentIndex:(NSInteger)currentIndex NS_DESIGNATED_INITIALIZER; 61 | 62 | - (instancetype)initWithFrame:(CGRect)frame 63 | style:(DNSPageStyle *)style 64 | titles:(NSArray *)titles 65 | childViewControllers:(NSArray *)childViewControllers; 66 | 67 | 68 | - (void)configureWithTitles:(nullable NSArray *)titles 69 | childViewControllers:(nullable NSArray *)childViewControllers 70 | style:(nullable DNSPageStyle *)style; 71 | 72 | @end 73 | 74 | NS_ASSUME_NONNULL_END 75 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageStyle.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageStyle.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | NS_ASSUME_NONNULL_BEGIN 30 | 31 | @interface DNSPageStyle : NSObject 32 | 33 | 34 | /// titleView 35 | @property (nonatomic, assign) CGFloat titleViewHeight; 36 | @property (nonatomic, strong) UIColor *titleColor; 37 | @property (nonatomic, strong) UIColor *titleSelectedColor; 38 | @property (nonatomic, strong) UIFont *titleFont; 39 | @property (nullable, nonatomic, strong) UIFont *titleSelectedFont; 40 | @property (nonatomic, strong) UIColor *titleViewBackgroundColor; 41 | @property (nonatomic, strong) UIColor *titleViewSelectedColor; 42 | @property (nonatomic, assign) CGFloat titleMargin; 43 | @property (nonatomic, assign) CGFloat titleInset; 44 | 45 | /// titleView 滑动 46 | @property (nonatomic, assign, getter=isTitleViewScrollEnabled) BOOL titleViewScrollEnabled; 47 | 48 | /// title 下划线 49 | @property (nonatomic, assign, getter=isShowBottomLine) BOOL showBottomLine; 50 | @property (nonatomic, strong) UIColor *bottomLineColor; 51 | @property (nonatomic, assign) CGFloat bottomLineWidth; 52 | @property (nonatomic, assign) CGFloat bottomLineHeight; 53 | @property (nonatomic, assign) CGFloat bottomLineRadius; 54 | 55 | /// title 缩放 56 | @property (nonatomic, assign, getter=isTitleScaleEnabled) BOOL titleScaleEnabled; 57 | @property (nonatomic, assign) CGFloat titleMaximumScaleFactor; 58 | 59 | /// title 遮罩 60 | @property (nonatomic, assign, getter=isShowCoverView) BOOL showCoverView; 61 | @property (nonatomic, strong) UIColor *coverViewBackgroundColor; 62 | @property (nonatomic, assign) CGFloat coverViewAlpha; 63 | @property (nonatomic, assign) CGFloat coverMargin; 64 | @property (nonatomic, assign) CGFloat coverViewHeight; 65 | @property (nonatomic, assign) CGFloat coverViewRadius; 66 | 67 | /// contentView 68 | @property (nonatomic, assign, getter=isContentScrollEnabled) BOOL contentScrollEnabled; 69 | @property (nonatomic, strong) UIColor *contentViewBackgroundColor; 70 | 71 | NS_ASSUME_NONNULL_END 72 | 73 | @end 74 | 75 | 76 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageContentView.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageContentView.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels on 2018/9/24. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | #import "DNSPageStyle.h" 29 | #import "DNSPageDelegate.h" 30 | 31 | NS_ASSUME_NONNULL_BEGIN 32 | 33 | 34 | @interface DNSPageContentView : UIView 35 | 36 | @property (nullable, nonatomic, weak) id delegate; 37 | 38 | @property (nullable, nonatomic, weak) id container; 39 | 40 | @property (nullable, nonatomic, weak) id eventHandler; 41 | 42 | @property (nonatomic, strong, readonly) DNSPageStyle *style; 43 | 44 | @property (nonatomic, strong, readonly) NSArray *childViewControllers; 45 | 46 | @property (nonatomic, assign, readonly) NSInteger currentIndex; 47 | 48 | @property (nonatomic, strong, readonly) UICollectionView *collectionView; 49 | 50 | - (instancetype)init NS_UNAVAILABLE; 51 | 52 | - (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; 53 | 54 | - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; 55 | 56 | - (instancetype)initWithFrame:(CGRect)frame 57 | style:(DNSPageStyle *)style 58 | childViewControllers:(NSArray *)childViewControllers 59 | currentIndex:(NSInteger)currentIndex NS_DESIGNATED_INITIALIZER; 60 | 61 | - (instancetype)initWithFrame:(CGRect)frame 62 | style:(DNSPageStyle *)style 63 | childViewControllers:(NSArray *)childViewControllers; 64 | 65 | - (void)configureWithChildViewControllers:(nullable NSArray *)childViewControllers 66 | style:(nullable DNSPageStyle *)style 67 | currentIndex:(NSInteger)currentIndex; 68 | 69 | - (void)configureWithChildViewControllers:(nullable NSArray *)childViewControllers 70 | style:(nullable DNSPageStyle *)style; 71 | 72 | @end 73 | 74 | NS_ASSUME_NONNULL_END 75 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageViewManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageViewManager.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | #import "DNSPageStyle.h" 29 | #import "DNSPageDelegate.h" 30 | 31 | @class DNSPageTitleView, DNSPageContentView; 32 | 33 | NS_ASSUME_NONNULL_BEGIN 34 | 35 | 36 | /// 通过这个类创建的 pageView,titleView 和 contentView 的 frame 是不确定的,适合于 titleView 和 contentView 分开布局的情况 37 | /// 需要给 titleView 和 contentView 布局,可以使用 frame 或者 Autolayout 38 | @interface DNSPageViewManager : NSObject 39 | 40 | @property (nonatomic, strong, readonly) DNSPageStyle *style; 41 | 42 | @property (nonatomic, strong, readonly) NSArray *titles; 43 | 44 | @property (nonatomic, strong, readonly) NSArray *childViewControllers; 45 | 46 | @property (nonatomic, assign, readonly) NSInteger currentIndex; 47 | 48 | @property (nonatomic, strong, readonly) DNSPageTitleView *titleView; 49 | 50 | @property (nonatomic, strong, readonly) DNSPageContentView *contentView; 51 | 52 | - (instancetype)init NS_UNAVAILABLE; 53 | 54 | - (instancetype)initWithStyle:(DNSPageStyle *)style 55 | titles:(NSArray *)titles 56 | childViewControllers:(NSArray *)childViewControllers 57 | currentIndex:(NSInteger)currentIndex 58 | titleView:(nullable DNSPageTitleView *)titleView 59 | contentView:(nullable DNSPageContentView *)contentView NS_DESIGNATED_INITIALIZER; 60 | 61 | - (instancetype)initWithStyle:(DNSPageStyle *)style 62 | titles:(NSArray *)titles 63 | childViewControllers:(NSArray *)childViewControllers 64 | currentIndex:(NSInteger)currentIndex; 65 | 66 | - (instancetype)initWithStyle:(DNSPageStyle *)style 67 | titles:(NSArray *)titles 68 | childViewControllers:(NSArray *)childViewControllers; 69 | 70 | - (void)configureWithTitles:(nullable NSArray *)titles 71 | childViewControllers:(nullable NSArray *)childViewControllers 72 | style:(nullable DNSPageStyle *)style; 73 | 74 | @end 75 | 76 | NS_ASSUME_NONNULL_END 77 | -------------------------------------------------------------------------------- /Demo/ViewController4.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController4.m 3 | // Demo 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | 9 | #import "ViewController4.h" 10 | #import 11 | #import "ContentViewController.h" 12 | #import "UIColor+Random.h" 13 | 14 | #define MAS_SHORTHAND 15 | #define MAS_SHORTHAND_GLOBALS 16 | #import "Masonry.h" 17 | 18 | @interface ViewController4 () 19 | 20 | @property (nonatomic, strong) NSArray *titles; 21 | 22 | @property (nonatomic, strong) DNSPageViewManager *pageViewManager; 23 | 24 | 25 | @end 26 | 27 | @implementation ViewController4 28 | 29 | 30 | - (DNSPageViewManager *)pageViewManager { 31 | if (!_pageViewManager) { 32 | // 创建 DNSPageStyle,设置样式 33 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 34 | style.showBottomLine = YES; 35 | style.titleViewScrollEnabled = YES; 36 | style.titleViewBackgroundColor = [UIColor clearColor]; 37 | if (@available(iOS 13.0, *)) { 38 | style.titleSelectedColor = [UIColor dns_dynamicColorWithLightColor:[UIColor redColor] darkColor:[UIColor blueColor]]; 39 | style.titleColor = [UIColor dns_dynamicColorWithLightColor:[UIColor greenColor] darkColor:[UIColor orangeColor]]; 40 | } else { 41 | style.titleSelectedColor = [UIColor blackColor]; 42 | style.titleColor = [UIColor grayColor]; 43 | } 44 | style.bottomLineColor = [UIColor colorWithRed:0 / 255.0 green:143 / 255.0 blue:223 / 255.0 alpha:1.0]; 45 | style.bottomLineWidth = 20; 46 | 47 | // 设置标题内容 48 | NSArray *titles = @[@"微信支付", @"支付宝"]; 49 | 50 | // 创建每一页对应的 controller 51 | for (int i = 0; i < titles.count; i++) { 52 | ContentViewController *controller = [[ContentViewController alloc] init]; 53 | controller.view.backgroundColor = [UIColor randomColor]; 54 | controller.index = i; 55 | [self addChildViewController:controller]; 56 | } 57 | _pageViewManager = [[DNSPageViewManager alloc] initWithStyle:style 58 | titles:titles 59 | childViewControllers:self.childViewControllers]; 60 | } 61 | return _pageViewManager; 62 | } 63 | 64 | - (void)viewDidLoad { 65 | [super viewDidLoad]; 66 | 67 | if (@available(iOS 11, *)) { 68 | } else { 69 | self.automaticallyAdjustsScrollViewInsets = NO; 70 | } 71 | 72 | DNSPageTitleView *titleView = self.pageViewManager.titleView; 73 | [self.view addSubview:titleView]; 74 | 75 | 76 | // 单独设置 titleView 的大小和位置,可以使用 autolayout 或者 frame 77 | [titleView makeConstraints:^(MASConstraintMaker *make) { 78 | if (@available(iOS 11.0, *)) { 79 | make.top.equalTo(self.view.safeAreaLayoutGuideTop); 80 | } else { 81 | make.top.equalTo(self.mas_topLayoutGuideBottom); 82 | } 83 | make.leading.trailing.equalTo(self.view); 84 | make.height.equalTo(44); 85 | }]; 86 | 87 | // 单独设置 contentView 的大小和位置,可以使用 autolayout 或者 frame 88 | DNSPageContentView *contentView = self.pageViewManager.contentView; 89 | [self.view addSubview:contentView]; 90 | [contentView makeConstraints:^(MASConstraintMaker *make) { 91 | make.top.equalTo(titleView.bottom); 92 | make.leading.trailing.bottom.equalTo(self.view); 93 | }]; 94 | } 95 | 96 | 97 | @end 98 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageTitleView.h: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageTitleView.h 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/21. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | #import "DNSPageStyle.h" 29 | #import "DNSPageDelegate.h" 30 | 31 | NS_ASSUME_NONNULL_BEGIN 32 | 33 | typedef void (^TitleClickHandler)(DNSPageTitleView *titleView, NSInteger currentIndex); 34 | 35 | 36 | @interface DNSPageTitleView : UIView 37 | 38 | @property (nullable, nonatomic, weak) id delegate; 39 | 40 | @property (nullable, nonatomic, weak) id container; 41 | 42 | /// 点击标题时调用 43 | @property (nullable, nonatomic, copy) TitleClickHandler clickHandler; 44 | 45 | @property (nonatomic, assign, readonly) NSInteger currentIndex; 46 | 47 | @property (nonatomic, strong, readonly) NSArray *titleLabels; 48 | 49 | @property (nonatomic, strong, readonly) DNSPageStyle *style; 50 | 51 | @property (nonatomic, strong, readonly) NSArray *titles; 52 | 53 | @property (nonatomic, strong, readonly) UIView *coverView; 54 | 55 | - (instancetype)init NS_UNAVAILABLE; 56 | 57 | - (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; 58 | 59 | - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; 60 | 61 | - (instancetype)initWithFrame:(CGRect)frame 62 | style:(DNSPageStyle *)style 63 | titles:(NSArray *)titles 64 | currentIndex:(NSInteger)currentIndex NS_DESIGNATED_INITIALIZER; 65 | 66 | - (instancetype)initWithFrame:(CGRect)frame 67 | style:(DNSPageStyle *)style 68 | titles:(NSArray *)titles; 69 | 70 | - (void)configureWithTitles:(nullable NSArray *)titles 71 | style:(nullable DNSPageStyle *)style 72 | currentIndex:(NSInteger)currentIndex; 73 | 74 | - (void)configureWithTitles:(nullable NSArray *)titles 75 | style:(nullable DNSPageStyle *)style; 76 | 77 | 78 | 79 | /// 通过代码实现点了某个位置的 titleView 80 | /// @param index 需要点击的 titleView 的下标 81 | /// @param animated 是否需要动画 82 | - (void)selectedTitleAtIndex:(NSInteger)index animated:(BOOL)animated; 83 | 84 | 85 | - (void)selectedTitleAtIndex:(NSInteger)index; 86 | 87 | - (void)updateTitle:(NSString *)title atIndex:(NSInteger)index; 88 | 89 | @end 90 | 91 | 92 | NS_ASSUME_NONNULL_END 93 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageView.m: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageView.m 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "DNSPageView.h" 28 | #import "DNSPageTitleView.h" 29 | #import "DNSPageContentView.h" 30 | 31 | 32 | 33 | @interface DNSPageView () 34 | 35 | @property (nonatomic, strong) DNSPageStyle *style; 36 | 37 | @property (nonatomic, strong) NSArray *titles; 38 | 39 | @property (nonatomic, strong) NSArray *childViewControllers; 40 | 41 | @property (nonatomic, assign) NSInteger currentIndex; 42 | 43 | @property (nonatomic, strong) DNSPageTitleView *titleView; 44 | 45 | @property (nonatomic, strong) DNSPageContentView *contentView; 46 | 47 | @end 48 | 49 | @implementation DNSPageView 50 | 51 | 52 | - (instancetype)initWithFrame:(CGRect)frame 53 | style:(DNSPageStyle *)style 54 | titles:(NSArray *)titles 55 | childViewControllers:(NSArray *)childViewControllers 56 | currentIndex:(NSInteger)currentIndex { 57 | NSParameterAssert(titles.count == childViewControllers.count); 58 | NSParameterAssert(currentIndex >= 0 && currentIndex < titles.count); 59 | self = [super initWithFrame:frame]; 60 | if (self) { 61 | self.style = style; 62 | self.titles = titles; 63 | self.childViewControllers = childViewControllers; 64 | self.currentIndex = currentIndex; 65 | self.titleView = [[DNSPageTitleView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, style.titleViewHeight) 66 | style:self.style 67 | titles:self.titles 68 | currentIndex:self.currentIndex]; 69 | self.contentView = [[DNSPageContentView alloc] initWithFrame:CGRectMake(0, style.titleViewHeight, frame.size.width, frame.size.height - style.titleViewHeight) 70 | style:self.style 71 | childViewControllers:self.childViewControllers 72 | currentIndex:self.currentIndex]; 73 | [self addSubview:self.titleView]; 74 | [self addSubview:self.contentView]; 75 | self.titleView.container = self; 76 | self.contentView.container = self; 77 | self.titleView.delegate = self.contentView; 78 | self.contentView.delegate = self.titleView; 79 | } 80 | return self; 81 | } 82 | 83 | - (instancetype)initWithFrame:(CGRect)frame 84 | style:(DNSPageStyle *)style 85 | titles:(NSArray *)titles 86 | childViewControllers:(NSArray *)childViewControllers { 87 | return [self initWithFrame:frame style:style titles:titles childViewControllers:childViewControllers currentIndex:0]; 88 | } 89 | 90 | - (void)configureWithTitles:(NSArray *)titles 91 | childViewControllers:(NSArray *)childViewControllers 92 | style:(DNSPageStyle *)style { 93 | if (titles) { 94 | self.titles = titles; 95 | } 96 | if (childViewControllers) { 97 | self.childViewControllers = childViewControllers; 98 | } 99 | if (style) { 100 | self.style = style; 101 | } 102 | NSParameterAssert(self.titles.count == self.childViewControllers.count); 103 | NSParameterAssert(self.currentIndex >= 0 && self.currentIndex < self.titles.count); 104 | [self.titleView configureWithTitles:titles style:style]; 105 | [self.contentView configureWithChildViewControllers:childViewControllers style:style]; 106 | } 107 | 108 | - (void)updateCurrentIndex:(NSInteger)index { 109 | self.currentIndex = index; 110 | } 111 | 112 | @end 113 | -------------------------------------------------------------------------------- /Masonry/View+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | /** 14 | * Provides constraint maker block 15 | * and convience methods for creating MASViewAttribute which are view + NSLayoutAttribute pairs 16 | */ 17 | @interface MAS_VIEW (MASAdditions) 18 | 19 | /** 20 | * following properties return a new MASViewAttribute with current view and appropriate NSLayoutAttribute 21 | */ 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_left; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_top; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_right; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leading; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_width; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_height; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX; 31 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY; 32 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline; 33 | @property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr); 34 | 35 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 36 | 37 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_firstBaseline; 38 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_lastBaseline; 39 | 40 | #endif 41 | 42 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 43 | 44 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin; 45 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin; 46 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin; 47 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin; 48 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin; 49 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin; 50 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins; 51 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins; 52 | 53 | #endif 54 | 55 | #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000) || (__TV_OS_VERSION_MAX_ALLOWED >= 110000) 56 | 57 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuide API_AVAILABLE(ios(11.0),tvos(11.0)); 58 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTop API_AVAILABLE(ios(11.0),tvos(11.0)); 59 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideBottom API_AVAILABLE(ios(11.0),tvos(11.0)); 60 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeft API_AVAILABLE(ios(11.0),tvos(11.0)); 61 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideRight API_AVAILABLE(ios(11.0),tvos(11.0)); 62 | 63 | #endif 64 | 65 | /** 66 | * a key to associate with this view 67 | */ 68 | @property (nonatomic, strong) id mas_key; 69 | 70 | /** 71 | * Finds the closest common superview between this view and another view 72 | * 73 | * @param view other view 74 | * 75 | * @return returns nil if common superview could not be found 76 | */ 77 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view; 78 | 79 | /** 80 | * Creates a MASConstraintMaker with the callee view. 81 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing 82 | * 83 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 84 | * 85 | * @return Array of created MASConstraints 86 | */ 87 | - (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 88 | 89 | /** 90 | * Creates a MASConstraintMaker with the callee view. 91 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 92 | * If an existing constraint exists then it will be updated instead. 93 | * 94 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 95 | * 96 | * @return Array of created/updated MASConstraints 97 | */ 98 | - (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 99 | 100 | /** 101 | * Creates a MASConstraintMaker with the callee view. 102 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 103 | * All constraints previously installed for the view will be removed. 104 | * 105 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 106 | * 107 | * @return Array of created/updated MASConstraints 108 | */ 109 | - (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block; 110 | 111 | @end 112 | -------------------------------------------------------------------------------- /Masonry/View+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand view additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface MAS_VIEW (MASShorthandAdditions) 18 | 19 | @property (nonatomic, strong, readonly) MASViewAttribute *left; 20 | @property (nonatomic, strong, readonly) MASViewAttribute *top; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *right; 22 | @property (nonatomic, strong, readonly) MASViewAttribute *bottom; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *leading; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *trailing; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *width; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *height; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *centerX; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *centerY; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *baseline; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *(^attribute)(NSLayoutAttribute attr); 31 | 32 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 33 | 34 | @property (nonatomic, strong, readonly) MASViewAttribute *firstBaseline; 35 | @property (nonatomic, strong, readonly) MASViewAttribute *lastBaseline; 36 | 37 | #endif 38 | 39 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 40 | 41 | @property (nonatomic, strong, readonly) MASViewAttribute *leftMargin; 42 | @property (nonatomic, strong, readonly) MASViewAttribute *rightMargin; 43 | @property (nonatomic, strong, readonly) MASViewAttribute *topMargin; 44 | @property (nonatomic, strong, readonly) MASViewAttribute *bottomMargin; 45 | @property (nonatomic, strong, readonly) MASViewAttribute *leadingMargin; 46 | @property (nonatomic, strong, readonly) MASViewAttribute *trailingMargin; 47 | @property (nonatomic, strong, readonly) MASViewAttribute *centerXWithinMargins; 48 | @property (nonatomic, strong, readonly) MASViewAttribute *centerYWithinMargins; 49 | 50 | #endif 51 | 52 | #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000) || (__TV_OS_VERSION_MAX_ALLOWED >= 110000) 53 | 54 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideTop API_AVAILABLE(ios(11.0),tvos(11.0)); 55 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideBottom API_AVAILABLE(ios(11.0),tvos(11.0)); 56 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideLeft API_AVAILABLE(ios(11.0),tvos(11.0)); 57 | @property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideRight API_AVAILABLE(ios(11.0),tvos(11.0)); 58 | 59 | #endif 60 | 61 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; 62 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; 63 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; 64 | 65 | @end 66 | 67 | #define MAS_ATTR_FORWARD(attr) \ 68 | - (MASViewAttribute *)attr { \ 69 | return [self mas_##attr]; \ 70 | } 71 | 72 | @implementation MAS_VIEW (MASShorthandAdditions) 73 | 74 | MAS_ATTR_FORWARD(top); 75 | MAS_ATTR_FORWARD(left); 76 | MAS_ATTR_FORWARD(bottom); 77 | MAS_ATTR_FORWARD(right); 78 | MAS_ATTR_FORWARD(leading); 79 | MAS_ATTR_FORWARD(trailing); 80 | MAS_ATTR_FORWARD(width); 81 | MAS_ATTR_FORWARD(height); 82 | MAS_ATTR_FORWARD(centerX); 83 | MAS_ATTR_FORWARD(centerY); 84 | MAS_ATTR_FORWARD(baseline); 85 | 86 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 87 | 88 | MAS_ATTR_FORWARD(firstBaseline); 89 | MAS_ATTR_FORWARD(lastBaseline); 90 | 91 | #endif 92 | 93 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 94 | 95 | MAS_ATTR_FORWARD(leftMargin); 96 | MAS_ATTR_FORWARD(rightMargin); 97 | MAS_ATTR_FORWARD(topMargin); 98 | MAS_ATTR_FORWARD(bottomMargin); 99 | MAS_ATTR_FORWARD(leadingMargin); 100 | MAS_ATTR_FORWARD(trailingMargin); 101 | MAS_ATTR_FORWARD(centerXWithinMargins); 102 | MAS_ATTR_FORWARD(centerYWithinMargins); 103 | 104 | #endif 105 | 106 | #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000) || (__TV_OS_VERSION_MAX_ALLOWED >= 110000) 107 | 108 | MAS_ATTR_FORWARD(safeAreaLayoutGuideTop); 109 | MAS_ATTR_FORWARD(safeAreaLayoutGuideBottom); 110 | MAS_ATTR_FORWARD(safeAreaLayoutGuideLeft); 111 | MAS_ATTR_FORWARD(safeAreaLayoutGuideRight); 112 | 113 | #endif 114 | 115 | - (MASViewAttribute *(^)(NSLayoutAttribute))attribute { 116 | return [self mas_attribute]; 117 | } 118 | 119 | - (NSArray *)makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 120 | return [self mas_makeConstraints:block]; 121 | } 122 | 123 | - (NSArray *)updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 124 | return [self mas_updateConstraints:block]; 125 | } 126 | 127 | - (NSArray *)remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { 128 | return [self mas_remakeConstraints:block]; 129 | } 130 | 131 | @end 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /Masonry/MASCompositeConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASCompositeConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | 12 | @interface MASCompositeConstraint () 13 | 14 | @property (nonatomic, strong) id mas_key; 15 | @property (nonatomic, strong) NSMutableArray *childConstraints; 16 | 17 | @end 18 | 19 | @implementation MASCompositeConstraint 20 | 21 | - (id)initWithChildren:(NSArray *)children { 22 | self = [super init]; 23 | if (!self) return nil; 24 | 25 | _childConstraints = [children mutableCopy]; 26 | for (MASConstraint *constraint in _childConstraints) { 27 | constraint.delegate = self; 28 | } 29 | 30 | return self; 31 | } 32 | 33 | #pragma mark - MASConstraintDelegate 34 | 35 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 36 | NSUInteger index = [self.childConstraints indexOfObject:constraint]; 37 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 38 | [self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint]; 39 | } 40 | 41 | - (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 42 | id strongDelegate = self.delegate; 43 | MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 44 | newConstraint.delegate = self; 45 | [self.childConstraints addObject:newConstraint]; 46 | return newConstraint; 47 | } 48 | 49 | #pragma mark - NSLayoutConstraint multiplier proxies 50 | 51 | - (MASConstraint * (^)(CGFloat))multipliedBy { 52 | return ^id(CGFloat multiplier) { 53 | for (MASConstraint *constraint in self.childConstraints) { 54 | constraint.multipliedBy(multiplier); 55 | } 56 | return self; 57 | }; 58 | } 59 | 60 | - (MASConstraint * (^)(CGFloat))dividedBy { 61 | return ^id(CGFloat divider) { 62 | for (MASConstraint *constraint in self.childConstraints) { 63 | constraint.dividedBy(divider); 64 | } 65 | return self; 66 | }; 67 | } 68 | 69 | #pragma mark - MASLayoutPriority proxy 70 | 71 | - (MASConstraint * (^)(MASLayoutPriority))priority { 72 | return ^id(MASLayoutPriority priority) { 73 | for (MASConstraint *constraint in self.childConstraints) { 74 | constraint.priority(priority); 75 | } 76 | return self; 77 | }; 78 | } 79 | 80 | #pragma mark - NSLayoutRelation proxy 81 | 82 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 83 | return ^id(id attr, NSLayoutRelation relation) { 84 | for (MASConstraint *constraint in self.childConstraints.copy) { 85 | constraint.equalToWithRelation(attr, relation); 86 | } 87 | return self; 88 | }; 89 | } 90 | 91 | #pragma mark - attribute chaining 92 | 93 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 94 | [self constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 95 | return self; 96 | } 97 | 98 | #pragma mark - Animator proxy 99 | 100 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 101 | 102 | - (MASConstraint *)animator { 103 | for (MASConstraint *constraint in self.childConstraints) { 104 | [constraint animator]; 105 | } 106 | return self; 107 | } 108 | 109 | #endif 110 | 111 | #pragma mark - debug helpers 112 | 113 | - (MASConstraint * (^)(id))key { 114 | return ^id(id key) { 115 | self.mas_key = key; 116 | int i = 0; 117 | for (MASConstraint *constraint in self.childConstraints) { 118 | constraint.key([NSString stringWithFormat:@"%@[%d]", key, i++]); 119 | } 120 | return self; 121 | }; 122 | } 123 | 124 | #pragma mark - NSLayoutConstraint constant setters 125 | 126 | - (void)setInsets:(MASEdgeInsets)insets { 127 | for (MASConstraint *constraint in self.childConstraints) { 128 | constraint.insets = insets; 129 | } 130 | } 131 | 132 | - (void)setInset:(CGFloat)inset { 133 | for (MASConstraint *constraint in self.childConstraints) { 134 | constraint.inset = inset; 135 | } 136 | } 137 | 138 | - (void)setOffset:(CGFloat)offset { 139 | for (MASConstraint *constraint in self.childConstraints) { 140 | constraint.offset = offset; 141 | } 142 | } 143 | 144 | - (void)setSizeOffset:(CGSize)sizeOffset { 145 | for (MASConstraint *constraint in self.childConstraints) { 146 | constraint.sizeOffset = sizeOffset; 147 | } 148 | } 149 | 150 | - (void)setCenterOffset:(CGPoint)centerOffset { 151 | for (MASConstraint *constraint in self.childConstraints) { 152 | constraint.centerOffset = centerOffset; 153 | } 154 | } 155 | 156 | #pragma mark - MASConstraint 157 | 158 | - (void)activate { 159 | for (MASConstraint *constraint in self.childConstraints) { 160 | [constraint activate]; 161 | } 162 | } 163 | 164 | - (void)deactivate { 165 | for (MASConstraint *constraint in self.childConstraints) { 166 | [constraint deactivate]; 167 | } 168 | } 169 | 170 | - (void)install { 171 | for (MASConstraint *constraint in self.childConstraints) { 172 | constraint.updateExisting = self.updateExisting; 173 | [constraint install]; 174 | } 175 | } 176 | 177 | - (void)uninstall { 178 | for (MASConstraint *constraint in self.childConstraints) { 179 | [constraint uninstall]; 180 | } 181 | } 182 | 183 | @end 184 | -------------------------------------------------------------------------------- /Masonry/MASConstraintMaker.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintMaker.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | typedef NS_OPTIONS(NSInteger, MASAttribute) { 13 | MASAttributeLeft = 1 << NSLayoutAttributeLeft, 14 | MASAttributeRight = 1 << NSLayoutAttributeRight, 15 | MASAttributeTop = 1 << NSLayoutAttributeTop, 16 | MASAttributeBottom = 1 << NSLayoutAttributeBottom, 17 | MASAttributeLeading = 1 << NSLayoutAttributeLeading, 18 | MASAttributeTrailing = 1 << NSLayoutAttributeTrailing, 19 | MASAttributeWidth = 1 << NSLayoutAttributeWidth, 20 | MASAttributeHeight = 1 << NSLayoutAttributeHeight, 21 | MASAttributeCenterX = 1 << NSLayoutAttributeCenterX, 22 | MASAttributeCenterY = 1 << NSLayoutAttributeCenterY, 23 | MASAttributeBaseline = 1 << NSLayoutAttributeBaseline, 24 | 25 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 26 | 27 | MASAttributeFirstBaseline = 1 << NSLayoutAttributeFirstBaseline, 28 | MASAttributeLastBaseline = 1 << NSLayoutAttributeLastBaseline, 29 | 30 | #endif 31 | 32 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 33 | 34 | MASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin, 35 | MASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin, 36 | MASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin, 37 | MASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin, 38 | MASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin, 39 | MASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin, 40 | MASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins, 41 | MASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins, 42 | 43 | #endif 44 | 45 | }; 46 | 47 | /** 48 | * Provides factory methods for creating MASConstraints. 49 | * Constraints are collected until they are ready to be installed 50 | * 51 | */ 52 | @interface MASConstraintMaker : NSObject 53 | 54 | /** 55 | * The following properties return a new MASViewConstraint 56 | * with the first item set to the makers associated view and the appropriate MASViewAttribute 57 | */ 58 | @property (nonatomic, strong, readonly) MASConstraint *left; 59 | @property (nonatomic, strong, readonly) MASConstraint *top; 60 | @property (nonatomic, strong, readonly) MASConstraint *right; 61 | @property (nonatomic, strong, readonly) MASConstraint *bottom; 62 | @property (nonatomic, strong, readonly) MASConstraint *leading; 63 | @property (nonatomic, strong, readonly) MASConstraint *trailing; 64 | @property (nonatomic, strong, readonly) MASConstraint *width; 65 | @property (nonatomic, strong, readonly) MASConstraint *height; 66 | @property (nonatomic, strong, readonly) MASConstraint *centerX; 67 | @property (nonatomic, strong, readonly) MASConstraint *centerY; 68 | @property (nonatomic, strong, readonly) MASConstraint *baseline; 69 | 70 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 71 | 72 | @property (nonatomic, strong, readonly) MASConstraint *firstBaseline; 73 | @property (nonatomic, strong, readonly) MASConstraint *lastBaseline; 74 | 75 | #endif 76 | 77 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 78 | 79 | @property (nonatomic, strong, readonly) MASConstraint *leftMargin; 80 | @property (nonatomic, strong, readonly) MASConstraint *rightMargin; 81 | @property (nonatomic, strong, readonly) MASConstraint *topMargin; 82 | @property (nonatomic, strong, readonly) MASConstraint *bottomMargin; 83 | @property (nonatomic, strong, readonly) MASConstraint *leadingMargin; 84 | @property (nonatomic, strong, readonly) MASConstraint *trailingMargin; 85 | @property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins; 86 | @property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins; 87 | 88 | #endif 89 | 90 | /** 91 | * Returns a block which creates a new MASCompositeConstraint with the first item set 92 | * to the makers associated view and children corresponding to the set bits in the 93 | * MASAttribute parameter. Combine multiple attributes via binary-or. 94 | */ 95 | @property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs); 96 | 97 | /** 98 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeEdges 99 | * which generates the appropriate MASViewConstraint children (top, left, bottom, right) 100 | * with the first item set to the makers associated view 101 | */ 102 | @property (nonatomic, strong, readonly) MASConstraint *edges; 103 | 104 | /** 105 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeSize 106 | * which generates the appropriate MASViewConstraint children (width, height) 107 | * with the first item set to the makers associated view 108 | */ 109 | @property (nonatomic, strong, readonly) MASConstraint *size; 110 | 111 | /** 112 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeCenter 113 | * which generates the appropriate MASViewConstraint children (centerX, centerY) 114 | * with the first item set to the makers associated view 115 | */ 116 | @property (nonatomic, strong, readonly) MASConstraint *center; 117 | 118 | /** 119 | * Whether or not to check for an existing constraint instead of adding constraint 120 | */ 121 | @property (nonatomic, assign) BOOL updateExisting; 122 | 123 | /** 124 | * Whether or not to remove existing constraints prior to installing 125 | */ 126 | @property (nonatomic, assign) BOOL removeExisting; 127 | 128 | /** 129 | * initialises the maker with a default view 130 | * 131 | * @param view any MASConstraint are created with this view as the first item 132 | * 133 | * @return a new MASConstraintMaker 134 | */ 135 | - (id)initWithView:(MAS_VIEW *)view; 136 | 137 | /** 138 | * Calls install method on any MASConstraints which have been created by this maker 139 | * 140 | * @return an array of all the installed MASConstraints 141 | */ 142 | - (NSArray *)install; 143 | 144 | - (MASConstraint * (^)(dispatch_block_t))group; 145 | 146 | @end 147 | -------------------------------------------------------------------------------- /Masonry/NSLayoutConstraint+MASDebugAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSLayoutConstraint+MASDebugAdditions.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | 13 | @implementation NSLayoutConstraint (MASDebugAdditions) 14 | 15 | #pragma mark - description maps 16 | 17 | + (NSDictionary *)layoutRelationDescriptionsByValue { 18 | static dispatch_once_t once; 19 | static NSDictionary *descriptionMap; 20 | dispatch_once(&once, ^{ 21 | descriptionMap = @{ 22 | @(NSLayoutRelationEqual) : @"==", 23 | @(NSLayoutRelationGreaterThanOrEqual) : @">=", 24 | @(NSLayoutRelationLessThanOrEqual) : @"<=", 25 | }; 26 | }); 27 | return descriptionMap; 28 | } 29 | 30 | + (NSDictionary *)layoutAttributeDescriptionsByValue { 31 | static dispatch_once_t once; 32 | static NSDictionary *descriptionMap; 33 | dispatch_once(&once, ^{ 34 | descriptionMap = @{ 35 | @(NSLayoutAttributeTop) : @"top", 36 | @(NSLayoutAttributeLeft) : @"left", 37 | @(NSLayoutAttributeBottom) : @"bottom", 38 | @(NSLayoutAttributeRight) : @"right", 39 | @(NSLayoutAttributeLeading) : @"leading", 40 | @(NSLayoutAttributeTrailing) : @"trailing", 41 | @(NSLayoutAttributeWidth) : @"width", 42 | @(NSLayoutAttributeHeight) : @"height", 43 | @(NSLayoutAttributeCenterX) : @"centerX", 44 | @(NSLayoutAttributeCenterY) : @"centerY", 45 | @(NSLayoutAttributeBaseline) : @"baseline", 46 | 47 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 48 | @(NSLayoutAttributeFirstBaseline) : @"firstBaseline", 49 | @(NSLayoutAttributeLastBaseline) : @"lastBaseline", 50 | #endif 51 | 52 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 53 | @(NSLayoutAttributeLeftMargin) : @"leftMargin", 54 | @(NSLayoutAttributeRightMargin) : @"rightMargin", 55 | @(NSLayoutAttributeTopMargin) : @"topMargin", 56 | @(NSLayoutAttributeBottomMargin) : @"bottomMargin", 57 | @(NSLayoutAttributeLeadingMargin) : @"leadingMargin", 58 | @(NSLayoutAttributeTrailingMargin) : @"trailingMargin", 59 | @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins", 60 | @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins", 61 | #endif 62 | 63 | }; 64 | 65 | }); 66 | return descriptionMap; 67 | } 68 | 69 | 70 | + (NSDictionary *)layoutPriorityDescriptionsByValue { 71 | static dispatch_once_t once; 72 | static NSDictionary *descriptionMap; 73 | dispatch_once(&once, ^{ 74 | #if TARGET_OS_IPHONE || TARGET_OS_TV 75 | descriptionMap = @{ 76 | @(MASLayoutPriorityDefaultHigh) : @"high", 77 | @(MASLayoutPriorityDefaultLow) : @"low", 78 | @(MASLayoutPriorityDefaultMedium) : @"medium", 79 | @(MASLayoutPriorityRequired) : @"required", 80 | @(MASLayoutPriorityFittingSizeLevel) : @"fitting size", 81 | }; 82 | #elif TARGET_OS_MAC 83 | descriptionMap = @{ 84 | @(MASLayoutPriorityDefaultHigh) : @"high", 85 | @(MASLayoutPriorityDragThatCanResizeWindow) : @"drag can resize window", 86 | @(MASLayoutPriorityDefaultMedium) : @"medium", 87 | @(MASLayoutPriorityWindowSizeStayPut) : @"window size stay put", 88 | @(MASLayoutPriorityDragThatCannotResizeWindow) : @"drag cannot resize window", 89 | @(MASLayoutPriorityDefaultLow) : @"low", 90 | @(MASLayoutPriorityFittingSizeCompression) : @"fitting size", 91 | @(MASLayoutPriorityRequired) : @"required", 92 | }; 93 | #endif 94 | }); 95 | return descriptionMap; 96 | } 97 | 98 | #pragma mark - description override 99 | 100 | + (NSString *)descriptionForObject:(id)obj { 101 | if ([obj respondsToSelector:@selector(mas_key)] && [obj mas_key]) { 102 | return [NSString stringWithFormat:@"%@:%@", [obj class], [obj mas_key]]; 103 | } 104 | return [NSString stringWithFormat:@"%@:%p", [obj class], obj]; 105 | } 106 | 107 | - (NSString *)description { 108 | NSMutableString *description = [[NSMutableString alloc] initWithString:@"<"]; 109 | 110 | [description appendString:[self.class descriptionForObject:self]]; 111 | 112 | [description appendFormat:@" %@", [self.class descriptionForObject:self.firstItem]]; 113 | if (self.firstAttribute != NSLayoutAttributeNotAnAttribute) { 114 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.firstAttribute)]]; 115 | } 116 | 117 | [description appendFormat:@" %@", self.class.layoutRelationDescriptionsByValue[@(self.relation)]]; 118 | 119 | if (self.secondItem) { 120 | [description appendFormat:@" %@", [self.class descriptionForObject:self.secondItem]]; 121 | } 122 | if (self.secondAttribute != NSLayoutAttributeNotAnAttribute) { 123 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.secondAttribute)]]; 124 | } 125 | 126 | if (self.multiplier != 1) { 127 | [description appendFormat:@" * %g", self.multiplier]; 128 | } 129 | 130 | if (self.secondAttribute == NSLayoutAttributeNotAnAttribute) { 131 | [description appendFormat:@" %g", self.constant]; 132 | } else { 133 | if (self.constant) { 134 | [description appendFormat:@" %@ %g", (self.constant < 0 ? @"-" : @"+"), ABS(self.constant)]; 135 | } 136 | } 137 | 138 | if (self.priority != MASLayoutPriorityRequired) { 139 | [description appendFormat:@" ^%@", self.class.layoutPriorityDescriptionsByValue[@(self.priority)] ?: [NSNumber numberWithDouble:self.priority]]; 140 | } 141 | 142 | [description appendString:@">"]; 143 | return description; 144 | } 145 | 146 | @end 147 | -------------------------------------------------------------------------------- /Masonry/MASUtilities.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASUtilities.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 19/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | 13 | #if TARGET_OS_IPHONE || TARGET_OS_TV 14 | 15 | #import 16 | #define MAS_VIEW UIView 17 | #define MAS_VIEW_CONTROLLER UIViewController 18 | #define MASEdgeInsets UIEdgeInsets 19 | 20 | typedef UILayoutPriority MASLayoutPriority; 21 | static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired; 22 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh; 23 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500; 24 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow; 25 | static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel; 26 | 27 | #elif TARGET_OS_MAC 28 | 29 | #import 30 | #define MAS_VIEW NSView 31 | #define MASEdgeInsets NSEdgeInsets 32 | 33 | typedef NSLayoutPriority MASLayoutPriority; 34 | static const MASLayoutPriority MASLayoutPriorityRequired = NSLayoutPriorityRequired; 35 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = NSLayoutPriorityDefaultHigh; 36 | static const MASLayoutPriority MASLayoutPriorityDragThatCanResizeWindow = NSLayoutPriorityDragThatCanResizeWindow; 37 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 501; 38 | static const MASLayoutPriority MASLayoutPriorityWindowSizeStayPut = NSLayoutPriorityWindowSizeStayPut; 39 | static const MASLayoutPriority MASLayoutPriorityDragThatCannotResizeWindow = NSLayoutPriorityDragThatCannotResizeWindow; 40 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = NSLayoutPriorityDefaultLow; 41 | static const MASLayoutPriority MASLayoutPriorityFittingSizeCompression = NSLayoutPriorityFittingSizeCompression; 42 | 43 | #endif 44 | 45 | /** 46 | * Allows you to attach keys to objects matching the variable names passed. 47 | * 48 | * view1.mas_key = @"view1", view2.mas_key = @"view2"; 49 | * 50 | * is equivalent to: 51 | * 52 | * MASAttachKeys(view1, view2); 53 | */ 54 | #define MASAttachKeys(...) \ 55 | { \ 56 | NSDictionary *keyPairs = NSDictionaryOfVariableBindings(__VA_ARGS__); \ 57 | for (id key in keyPairs.allKeys) { \ 58 | id obj = keyPairs[key]; \ 59 | NSAssert([obj respondsToSelector:@selector(setMas_key:)], \ 60 | @"Cannot attach mas_key to %@", obj); \ 61 | [obj setMas_key:key]; \ 62 | } \ 63 | } 64 | 65 | /** 66 | * Used to create object hashes 67 | * Based on http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html 68 | */ 69 | #define MAS_NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger)) 70 | #define MAS_NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (MAS_NSUINT_BIT - howmuch))) 71 | 72 | /** 73 | * Given a scalar or struct value, wraps it in NSValue 74 | * Based on EXPObjectify: https://github.com/specta/expecta 75 | */ 76 | static inline id _MASBoxValue(const char *type, ...) { 77 | va_list v; 78 | va_start(v, type); 79 | id obj = nil; 80 | if (strcmp(type, @encode(id)) == 0) { 81 | id actual = va_arg(v, id); 82 | obj = actual; 83 | } else if (strcmp(type, @encode(CGPoint)) == 0) { 84 | CGPoint actual = (CGPoint)va_arg(v, CGPoint); 85 | obj = [NSValue value:&actual withObjCType:type]; 86 | } else if (strcmp(type, @encode(CGSize)) == 0) { 87 | CGSize actual = (CGSize)va_arg(v, CGSize); 88 | obj = [NSValue value:&actual withObjCType:type]; 89 | } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) { 90 | MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets); 91 | obj = [NSValue value:&actual withObjCType:type]; 92 | } else if (strcmp(type, @encode(double)) == 0) { 93 | double actual = (double)va_arg(v, double); 94 | obj = [NSNumber numberWithDouble:actual]; 95 | } else if (strcmp(type, @encode(float)) == 0) { 96 | float actual = (float)va_arg(v, double); 97 | obj = [NSNumber numberWithFloat:actual]; 98 | } else if (strcmp(type, @encode(int)) == 0) { 99 | int actual = (int)va_arg(v, int); 100 | obj = [NSNumber numberWithInt:actual]; 101 | } else if (strcmp(type, @encode(long)) == 0) { 102 | long actual = (long)va_arg(v, long); 103 | obj = [NSNumber numberWithLong:actual]; 104 | } else if (strcmp(type, @encode(long long)) == 0) { 105 | long long actual = (long long)va_arg(v, long long); 106 | obj = [NSNumber numberWithLongLong:actual]; 107 | } else if (strcmp(type, @encode(short)) == 0) { 108 | short actual = (short)va_arg(v, int); 109 | obj = [NSNumber numberWithShort:actual]; 110 | } else if (strcmp(type, @encode(char)) == 0) { 111 | char actual = (char)va_arg(v, int); 112 | obj = [NSNumber numberWithChar:actual]; 113 | } else if (strcmp(type, @encode(bool)) == 0) { 114 | bool actual = (bool)va_arg(v, int); 115 | obj = [NSNumber numberWithBool:actual]; 116 | } else if (strcmp(type, @encode(unsigned char)) == 0) { 117 | unsigned char actual = (unsigned char)va_arg(v, unsigned int); 118 | obj = [NSNumber numberWithUnsignedChar:actual]; 119 | } else if (strcmp(type, @encode(unsigned int)) == 0) { 120 | unsigned int actual = (unsigned int)va_arg(v, unsigned int); 121 | obj = [NSNumber numberWithUnsignedInt:actual]; 122 | } else if (strcmp(type, @encode(unsigned long)) == 0) { 123 | unsigned long actual = (unsigned long)va_arg(v, unsigned long); 124 | obj = [NSNumber numberWithUnsignedLong:actual]; 125 | } else if (strcmp(type, @encode(unsigned long long)) == 0) { 126 | unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long); 127 | obj = [NSNumber numberWithUnsignedLongLong:actual]; 128 | } else if (strcmp(type, @encode(unsigned short)) == 0) { 129 | unsigned short actual = (unsigned short)va_arg(v, unsigned int); 130 | obj = [NSNumber numberWithUnsignedShort:actual]; 131 | } 132 | va_end(v); 133 | return obj; 134 | } 135 | 136 | #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value)) 137 | -------------------------------------------------------------------------------- /Masonry/NSArray+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.m 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | #import "View+MASAdditions.h" 11 | 12 | @implementation NSArray (MASAdditions) 13 | 14 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block { 15 | NSMutableArray *constraints = [NSMutableArray array]; 16 | for (MAS_VIEW *view in self) { 17 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 18 | [constraints addObjectsFromArray:[view mas_makeConstraints:block]]; 19 | } 20 | return constraints; 21 | } 22 | 23 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block { 24 | NSMutableArray *constraints = [NSMutableArray array]; 25 | for (MAS_VIEW *view in self) { 26 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 27 | [constraints addObjectsFromArray:[view mas_updateConstraints:block]]; 28 | } 29 | return constraints; 30 | } 31 | 32 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block { 33 | NSMutableArray *constraints = [NSMutableArray array]; 34 | for (MAS_VIEW *view in self) { 35 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 36 | [constraints addObjectsFromArray:[view mas_remakeConstraints:block]]; 37 | } 38 | return constraints; 39 | } 40 | 41 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 42 | if (self.count < 2) { 43 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 44 | return; 45 | } 46 | 47 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 48 | if (axisType == MASAxisTypeHorizontal) { 49 | MAS_VIEW *prev; 50 | for (int i = 0; i < self.count; i++) { 51 | MAS_VIEW *v = self[i]; 52 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 53 | if (prev) { 54 | make.width.equalTo(prev); 55 | make.left.equalTo(prev.mas_right).offset(fixedSpacing); 56 | if (i == self.count - 1) {//last one 57 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 58 | } 59 | } 60 | else {//first one 61 | make.left.equalTo(tempSuperView).offset(leadSpacing); 62 | } 63 | 64 | }]; 65 | prev = v; 66 | } 67 | } 68 | else { 69 | MAS_VIEW *prev; 70 | for (int i = 0; i < self.count; i++) { 71 | MAS_VIEW *v = self[i]; 72 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 73 | if (prev) { 74 | make.height.equalTo(prev); 75 | make.top.equalTo(prev.mas_bottom).offset(fixedSpacing); 76 | if (i == self.count - 1) {//last one 77 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 78 | } 79 | } 80 | else {//first one 81 | make.top.equalTo(tempSuperView).offset(leadSpacing); 82 | } 83 | 84 | }]; 85 | prev = v; 86 | } 87 | } 88 | } 89 | 90 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 91 | if (self.count < 2) { 92 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 93 | return; 94 | } 95 | 96 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 97 | if (axisType == MASAxisTypeHorizontal) { 98 | MAS_VIEW *prev; 99 | for (int i = 0; i < self.count; i++) { 100 | MAS_VIEW *v = self[i]; 101 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 102 | make.width.equalTo(@(fixedItemLength)); 103 | if (prev) { 104 | if (i == self.count - 1) {//last one 105 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 106 | } 107 | else { 108 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 109 | make.right.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 110 | } 111 | } 112 | else {//first one 113 | make.left.equalTo(tempSuperView).offset(leadSpacing); 114 | } 115 | }]; 116 | prev = v; 117 | } 118 | } 119 | else { 120 | MAS_VIEW *prev; 121 | for (int i = 0; i < self.count; i++) { 122 | MAS_VIEW *v = self[i]; 123 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 124 | make.height.equalTo(@(fixedItemLength)); 125 | if (prev) { 126 | if (i == self.count - 1) {//last one 127 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 128 | } 129 | else { 130 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 131 | make.bottom.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 132 | } 133 | } 134 | else {//first one 135 | make.top.equalTo(tempSuperView).offset(leadSpacing); 136 | } 137 | }]; 138 | prev = v; 139 | } 140 | } 141 | } 142 | 143 | - (MAS_VIEW *)mas_commonSuperviewOfViews 144 | { 145 | MAS_VIEW *commonSuperview = nil; 146 | MAS_VIEW *previousView = nil; 147 | for (id object in self) { 148 | if ([object isKindOfClass:[MAS_VIEW class]]) { 149 | MAS_VIEW *view = (MAS_VIEW *)object; 150 | if (previousView) { 151 | commonSuperview = [view mas_closestCommonSuperview:commonSuperview]; 152 | } else { 153 | commonSuperview = view; 154 | } 155 | previousView = view; 156 | } 157 | } 158 | NSAssert(commonSuperview, @"Can't constrain views that do not share a common superview. Make sure that all the views in this array have been added into the same view hierarchy."); 159 | return commonSuperview; 160 | } 161 | 162 | @end 163 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageViewManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageViewManager.m 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels Lau on 2018/9/25. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "DNSPageViewManager.h" 28 | #import "DNSPageTitleView.h" 29 | #import "DNSPageContentView.h" 30 | 31 | @interface DNSPageViewManager () 32 | 33 | @property (nonatomic, strong) DNSPageStyle *style; 34 | 35 | @property (nonatomic, strong) NSArray *titles; 36 | 37 | @property (nonatomic, strong) NSArray *childViewControllers; 38 | 39 | @property (nonatomic, assign) NSInteger currentIndex; 40 | 41 | @property (nonatomic, strong) DNSPageTitleView *titleView; 42 | 43 | @property (nonatomic, strong) DNSPageContentView *contentView; 44 | 45 | @end 46 | 47 | @implementation DNSPageViewManager 48 | 49 | - (DNSPageTitleView *)titleView { 50 | if (!_titleView) { 51 | _titleView = [[DNSPageTitleView alloc] initWithFrame:CGRectZero 52 | style:self.style 53 | titles:self.titles 54 | currentIndex:self.currentIndex]; 55 | } 56 | return _titleView; 57 | } 58 | 59 | - (DNSPageContentView *)contentView { 60 | if (!_contentView) { 61 | _contentView = [[DNSPageContentView alloc] initWithFrame:CGRectZero 62 | style:self.style 63 | childViewControllers:self.childViewControllers 64 | currentIndex:self.currentIndex]; 65 | } 66 | return _contentView; 67 | } 68 | 69 | 70 | - (instancetype)initWithStyle:(DNSPageStyle *)style 71 | titles:(NSArray *)titles 72 | childViewControllers:(NSArray *)childViewControllers 73 | currentIndex:(NSInteger)currentIndex 74 | titleView:(DNSPageTitleView *)titleView 75 | contentView:(DNSPageContentView *)contentView { 76 | NSParameterAssert(titles.count == childViewControllers.count); 77 | NSParameterAssert(currentIndex >= 0 && currentIndex < titles.count); 78 | self = [super init]; 79 | if (self) { 80 | self.style = style; 81 | self.titles = titles; 82 | self.childViewControllers = childViewControllers; 83 | self.currentIndex = currentIndex; 84 | if (titleView) { 85 | self.titleView = titleView; 86 | [self.titleView configureWithTitles:titles style:style currentIndex:currentIndex]; 87 | } else { 88 | self.titleView = [[DNSPageTitleView alloc] initWithFrame:CGRectZero style:style titles:titles currentIndex:currentIndex]; 89 | } 90 | if (contentView) { 91 | self.contentView = contentView; 92 | [self.contentView configureWithChildViewControllers:childViewControllers 93 | style:style 94 | currentIndex:currentIndex]; 95 | } else { 96 | self.contentView = [[DNSPageContentView alloc] initWithFrame:CGRectZero 97 | style:style 98 | childViewControllers:childViewControllers 99 | currentIndex:currentIndex]; 100 | } 101 | self.titleView.container = self; 102 | self.contentView.container = self; 103 | self.titleView.delegate = self.contentView; 104 | self.contentView.delegate = self.titleView; 105 | } 106 | return self; 107 | } 108 | 109 | - (instancetype)initWithStyle:(DNSPageStyle *)style 110 | titles:(NSArray *)titles 111 | childViewControllers:(NSArray *)childViewControllers 112 | currentIndex:(NSInteger)currentIndex { 113 | return [self initWithStyle:style 114 | titles:titles 115 | childViewControllers:childViewControllers 116 | currentIndex:currentIndex 117 | titleView:nil 118 | contentView:nil]; 119 | } 120 | 121 | - (instancetype)initWithStyle:(DNSPageStyle *)style 122 | titles:(NSArray *)titles 123 | childViewControllers:(NSArray *)childViewControllers { 124 | return [self initWithStyle:style 125 | titles:titles 126 | childViewControllers:childViewControllers 127 | currentIndex:0 128 | titleView:nil 129 | contentView:nil]; 130 | } 131 | 132 | - (void)configureWithTitles:(NSArray *)titles 133 | childViewControllers:(NSArray *)childViewControllers 134 | style:(DNSPageStyle *)style { 135 | if (titles) { 136 | self.titles = titles; 137 | } 138 | if (childViewControllers) { 139 | self.childViewControllers = childViewControllers; 140 | } 141 | if (style) { 142 | self.style = style; 143 | } 144 | NSParameterAssert(self.titles.count == self.childViewControllers.count); 145 | NSParameterAssert(self.currentIndex >= 0 && self.currentIndex < self.titles.count); 146 | [self.titleView configureWithTitles:titles style:style]; 147 | [self.contentView configureWithChildViewControllers:childViewControllers style:style]; 148 | } 149 | 150 | 151 | 152 | - (void)updateCurrentIndex:(NSInteger)index { 153 | self.currentIndex = index; 154 | } 155 | 156 | @end 157 | -------------------------------------------------------------------------------- /Masonry/View+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | #import 11 | 12 | @implementation MAS_VIEW (MASAdditions) 13 | 14 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { 15 | self.translatesAutoresizingMaskIntoConstraints = NO; 16 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 17 | block(constraintMaker); 18 | return [constraintMaker install]; 19 | } 20 | 21 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block { 22 | self.translatesAutoresizingMaskIntoConstraints = NO; 23 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 24 | constraintMaker.updateExisting = YES; 25 | block(constraintMaker); 26 | return [constraintMaker install]; 27 | } 28 | 29 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block { 30 | self.translatesAutoresizingMaskIntoConstraints = NO; 31 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 32 | constraintMaker.removeExisting = YES; 33 | block(constraintMaker); 34 | return [constraintMaker install]; 35 | } 36 | 37 | #pragma mark - NSLayoutAttribute properties 38 | 39 | - (MASViewAttribute *)mas_left { 40 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeft]; 41 | } 42 | 43 | - (MASViewAttribute *)mas_top { 44 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTop]; 45 | } 46 | 47 | - (MASViewAttribute *)mas_right { 48 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRight]; 49 | } 50 | 51 | - (MASViewAttribute *)mas_bottom { 52 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottom]; 53 | } 54 | 55 | - (MASViewAttribute *)mas_leading { 56 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeading]; 57 | } 58 | 59 | - (MASViewAttribute *)mas_trailing { 60 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailing]; 61 | } 62 | 63 | - (MASViewAttribute *)mas_width { 64 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeWidth]; 65 | } 66 | 67 | - (MASViewAttribute *)mas_height { 68 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeHeight]; 69 | } 70 | 71 | - (MASViewAttribute *)mas_centerX { 72 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterX]; 73 | } 74 | 75 | - (MASViewAttribute *)mas_centerY { 76 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterY]; 77 | } 78 | 79 | - (MASViewAttribute *)mas_baseline { 80 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBaseline]; 81 | } 82 | 83 | - (MASViewAttribute *(^)(NSLayoutAttribute))mas_attribute 84 | { 85 | return ^(NSLayoutAttribute attr) { 86 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:attr]; 87 | }; 88 | } 89 | 90 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 91 | 92 | - (MASViewAttribute *)mas_firstBaseline { 93 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeFirstBaseline]; 94 | } 95 | - (MASViewAttribute *)mas_lastBaseline { 96 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLastBaseline]; 97 | } 98 | 99 | #endif 100 | 101 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 102 | 103 | - (MASViewAttribute *)mas_leftMargin { 104 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeftMargin]; 105 | } 106 | 107 | - (MASViewAttribute *)mas_rightMargin { 108 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRightMargin]; 109 | } 110 | 111 | - (MASViewAttribute *)mas_topMargin { 112 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTopMargin]; 113 | } 114 | 115 | - (MASViewAttribute *)mas_bottomMargin { 116 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottomMargin]; 117 | } 118 | 119 | - (MASViewAttribute *)mas_leadingMargin { 120 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeadingMargin]; 121 | } 122 | 123 | - (MASViewAttribute *)mas_trailingMargin { 124 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailingMargin]; 125 | } 126 | 127 | - (MASViewAttribute *)mas_centerXWithinMargins { 128 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 129 | } 130 | 131 | - (MASViewAttribute *)mas_centerYWithinMargins { 132 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 133 | } 134 | 135 | #endif 136 | 137 | #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 110000) || (__TV_OS_VERSION_MAX_ALLOWED >= 110000) 138 | 139 | - (MASViewAttribute *)mas_safeAreaLayoutGuide { 140 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 141 | } 142 | - (MASViewAttribute *)mas_safeAreaLayoutGuideTop { 143 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 144 | } 145 | - (MASViewAttribute *)mas_safeAreaLayoutGuideBottom { 146 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 147 | } 148 | - (MASViewAttribute *)mas_safeAreaLayoutGuideLeft { 149 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeLeft]; 150 | } 151 | - (MASViewAttribute *)mas_safeAreaLayoutGuideRight { 152 | return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeRight]; 153 | } 154 | 155 | #endif 156 | 157 | #pragma mark - associated properties 158 | 159 | - (id)mas_key { 160 | return objc_getAssociatedObject(self, @selector(mas_key)); 161 | } 162 | 163 | - (void)setMas_key:(id)key { 164 | objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 165 | } 166 | 167 | #pragma mark - heirachy 168 | 169 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view { 170 | MAS_VIEW *closestCommonSuperview = nil; 171 | 172 | MAS_VIEW *secondViewSuperview = view; 173 | while (!closestCommonSuperview && secondViewSuperview) { 174 | MAS_VIEW *firstViewSuperview = self; 175 | while (!closestCommonSuperview && firstViewSuperview) { 176 | if (secondViewSuperview == firstViewSuperview) { 177 | closestCommonSuperview = secondViewSuperview; 178 | } 179 | firstViewSuperview = firstViewSuperview.superview; 180 | } 181 | secondViewSuperview = secondViewSuperview.superview; 182 | } 183 | return closestCommonSuperview; 184 | } 185 | 186 | @end 187 | -------------------------------------------------------------------------------- /Demo/ViewController5.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController5.m 3 | // Demo 4 | // 5 | // Created by Daniels on 2020/8/16. 6 | // Copyright © 2020 Daniels. All rights reserved. 7 | // 8 | 9 | #import "ViewController5.h" 10 | #import 11 | #import "ContentViewController.h" 12 | #import "UIColor+Random.h" 13 | 14 | #define MAS_SHORTHAND 15 | #define MAS_SHORTHAND_GLOBALS 16 | #import "Masonry.h" 17 | 18 | @interface ViewController5 () 19 | 20 | @property (nonatomic, assign, getter=isChanged) BOOL changed; 21 | 22 | @property (nonatomic, strong) NSArray *titles; 23 | 24 | @property (nonatomic, strong) DNSPageViewManager *pageViewManager; 25 | 26 | 27 | @end 28 | 29 | @implementation ViewController5 30 | 31 | 32 | - (DNSPageViewManager *)pageViewManager { 33 | if (!_pageViewManager) { 34 | // 创建 DNSPageStyle,设置样式 35 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 36 | style.showBottomLine = YES; 37 | style.titleViewScrollEnabled = YES; 38 | style.titleViewBackgroundColor = [UIColor clearColor]; 39 | if (@available(iOS 13.0, *)) { 40 | style.titleSelectedColor = [UIColor dns_dynamicColorWithLightColor:[UIColor redColor] darkColor:[UIColor blueColor]]; 41 | style.titleColor = [UIColor dns_dynamicColorWithLightColor:[UIColor greenColor] darkColor:[UIColor orangeColor]]; 42 | } else { 43 | style.titleSelectedColor = [UIColor blackColor]; 44 | style.titleColor = [UIColor grayColor]; 45 | } 46 | style.bottomLineColor = [UIColor colorWithRed:0 / 255.0 green:143 / 255.0 blue:223 / 255.0 alpha:1.0]; 47 | style.bottomLineWidth = 20; 48 | 49 | // 设置标题内容 50 | NSArray *titles = @[@"NBA", @"CBA", @"英雄联盟", @"王者荣耀", @"中国足球", @"国际足球"]; 51 | 52 | // 创建每一页对应的 controller 53 | for (int i = 0; i < titles.count; i++) { 54 | ContentViewController *controller = [[ContentViewController alloc] init]; 55 | controller.view.backgroundColor = [UIColor randomColor]; 56 | controller.index = i; 57 | [self addChildViewController:controller]; 58 | } 59 | _pageViewManager = [[DNSPageViewManager alloc] initWithStyle:style 60 | titles:titles 61 | childViewControllers:self.childViewControllers 62 | currentIndex:3]; 63 | } 64 | return _pageViewManager; 65 | } 66 | 67 | - (void)viewDidLoad { 68 | [super viewDidLoad]; 69 | 70 | if (@available(iOS 11, *)) { 71 | } else { 72 | self.automaticallyAdjustsScrollViewInsets = NO; 73 | } 74 | 75 | DNSPageTitleView *titleView = self.pageViewManager.titleView; 76 | [self.view addSubview:titleView]; 77 | 78 | 79 | // 单独设置 titleView 的大小和位置,可以使用 autolayout 或者 frame 80 | [titleView makeConstraints:^(MASConstraintMaker *make) { 81 | if (@available(iOS 11.0, *)) { 82 | make.top.equalTo(self.view.safeAreaLayoutGuideTop); 83 | } else { 84 | make.top.equalTo(self.mas_topLayoutGuideBottom); 85 | } 86 | make.leading.trailing.equalTo(self.view); 87 | make.height.equalTo(44); 88 | }]; 89 | 90 | // 单独设置 contentView 的大小和位置,可以使用 autolayout 或者 frame 91 | DNSPageContentView *contentView = self.pageViewManager.contentView; 92 | [self.view addSubview:contentView]; 93 | [contentView makeConstraints:^(MASConstraintMaker *make) { 94 | make.top.equalTo(titleView.bottom); 95 | make.leading.trailing.bottom.equalTo(self.view); 96 | }]; 97 | 98 | 99 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"改变" 100 | style:UIBarButtonItemStylePlain 101 | target:self 102 | action:@selector(changeStyle)]; 103 | 104 | } 105 | 106 | - (void)changeStyle { 107 | [self changeTitle]; 108 | 109 | // [self changeTitles]; 110 | 111 | // [self changeAll]; 112 | } 113 | 114 | - (void)changeTitle { 115 | if (!self.isChanged) { 116 | [self.pageViewManager.titleView updateTitle:@"JavaScript" atIndex:3]; 117 | } else { 118 | [self.pageViewManager.titleView updateTitle:@"王者荣耀" atIndex:3]; 119 | } 120 | self.changed = !self.isChanged; 121 | } 122 | 123 | - (void)changeTitles { 124 | if (!self.isChanged) { 125 | NSArray *titles = @[@"Swift", @"Objective-C", @"JavaScript", @"Python", @"C++", @"Go"]; 126 | [self.pageViewManager configureWithTitles:titles childViewControllers:nil style:nil]; 127 | } else { 128 | NSArray *titles = @[@"NBA", @"CBA", @"英雄联盟", @"王者荣耀", @"中国足球", @"国际足球"]; 129 | [self.pageViewManager configureWithTitles:titles childViewControllers:nil style:nil]; 130 | } 131 | self.changed = !self.isChanged; 132 | } 133 | 134 | - (void)changeAll { 135 | if (!self.isChanged) { 136 | 137 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 138 | style.titleViewBackgroundColor = [UIColor redColor]; 139 | style.showCoverView = YES; 140 | style.titleViewScrollEnabled = YES; 141 | 142 | NSArray *titles = @[@"头条", @"视频", @"娱乐", @"要问", @"体育", @"科技", @"汽车", @"时尚", @"图片", @"游戏", @"房产"]; 143 | 144 | [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 145 | [obj removeFromParentViewController]; 146 | }]; 147 | // 创建每一页对应的controller 148 | for (int i = 0; i < titles.count; i++) { 149 | ContentViewController *controller = [[ContentViewController alloc] init]; 150 | controller.view.backgroundColor = [UIColor randomColor]; 151 | controller.index = i; 152 | [self addChildViewController:controller]; 153 | } 154 | [self.pageViewManager configureWithTitles:titles 155 | childViewControllers:self.childViewControllers 156 | style:style]; 157 | } else { 158 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 159 | style.showBottomLine = YES; 160 | style.titleViewScrollEnabled = YES; 161 | style.titleViewBackgroundColor = [UIColor clearColor]; 162 | if (@available(iOS 13.0, *)) { 163 | style.titleSelectedColor = [UIColor dns_dynamicColorWithLightColor:[UIColor redColor] darkColor:[UIColor blueColor]]; 164 | style.titleColor = [UIColor dns_dynamicColorWithLightColor:[UIColor greenColor] darkColor:[UIColor orangeColor]]; 165 | } else { 166 | style.titleSelectedColor = [UIColor blackColor]; 167 | style.titleColor = [UIColor grayColor]; 168 | } 169 | style.bottomLineColor = [UIColor colorWithRed:0 / 255.0 green:143 / 255.0 blue:223 / 255.0 alpha:1.0]; 170 | style.bottomLineWidth = 20; 171 | 172 | // 设置标题内容 173 | NSArray *titles = @[@"NBA", @"CBA", @"英雄联盟", @"王者荣耀", @"中国足球", @"国际足球"]; 174 | 175 | [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 176 | [obj removeFromParentViewController]; 177 | }]; 178 | // 创建每一页对应的 controller 179 | for (int i = 0; i < titles.count; i++) { 180 | ContentViewController *controller = [[ContentViewController alloc] init]; 181 | controller.view.backgroundColor = [UIColor randomColor]; 182 | controller.index = i; 183 | [self addChildViewController:controller]; 184 | } 185 | [self.pageViewManager configureWithTitles:titles 186 | childViewControllers:self.childViewControllers 187 | style:style]; 188 | 189 | } 190 | self.changed = !self.isChanged; 191 | } 192 | 193 | @end 194 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DNSPageView-ObjC 2 | 3 | [![Version](https://img.shields.io/cocoapods/v/DNSPageView-ObjC.svg?style=flat)](http://cocoapods.org/pods/DNSPageView-ObjC) 4 | [![License](https://img.shields.io/cocoapods/l/DNSPageView-ObjC.svg?style=flat)](http://cocoapods.org/pods/DNSPageView-ObjC) 5 | [![Platform](https://img.shields.io/cocoapods/p/DNSPageView-ObjC.svg?style=flat)](http://cocoapods.org/pods/DNSPageView-ObjC) 6 | 7 | DNSPageView 的 Objective-C 版本,是一个灵活且易于使用的 pageView 框架,titleView 和 contentView 可以布局在任意地方,可以纯代码初始化,也可以使用 xib 或者 storyboard 初始化,并且提供了常见样式属性进行设置。 8 | 9 | 如果你使用的开发语言是 Swift,请使用 [DNSPageView](https://github.com/Danie1s/DNSPageView) 10 | 11 | - [Features](#features) 12 | - [Requirements](#requirements) 13 | - [Installation](#installation) 14 | - [Example](#example) 15 | - [Usage](#usage) 16 | - [直接使用 DNSPageView 初始化](#直接使用-dnspageview-初始化) 17 | - [使用 xib 或者 storyboard 初始化](#使用-xib-或者-storyboard-初始化) 18 | - [使用 DNSPageViewManager 初始化](#使用-dnspageviewmanager-初始化) 19 | - [样式](#样式) 20 | - [事件回调](#事件回调) 21 | - [常见问题](#常见问题) 22 | - [License](#license) 23 | 24 | ## Features: 25 | 26 | - [x] 使用简单 27 | - [x] 多种初始化方式 28 | - [x] 灵活布局 29 | - [x] 常见的样式 30 | - [x] 双击 titleView 的回调 31 | - [x] contentView 滑动监听 32 | - [x] 适配 iOS 13 Dark Mode 33 | - [x] 动态改变样式 34 | 35 | ## Requirements 36 | 37 | - iOS 8.0+ 38 | 39 | - Xcode 9.0+ 40 | 41 | 42 | 43 | ## Installation 44 | 45 | ### CocoaPods 46 | 47 | [CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command: 48 | 49 | ```bash 50 | $ gem install cocoapods 51 | ``` 52 | 53 | > CocoaPods 1.1+ is required to build DNSPageView. 54 | 55 | To integrate DNSPageView into your Xcode project using CocoaPods, specify it in your `Podfile`: 56 | 57 | ```ruby 58 | source 'https://github.com/CocoaPods/Specs.git' 59 | platform :ios, '8.0' 60 | 61 | target '' do 62 | pod 'DNSPageView-ObjC' 63 | end 64 | ``` 65 | 66 | Then, run the following command: 67 | 68 | ```bash 69 | $ pod install 70 | ``` 71 | 72 | 73 | ### Manually 74 | 75 | If you prefer not to use any of the aforementioned dependency managers, you can integrate DNSPageView-ObjC into your project manually. 76 | 77 | 78 | 79 | ## Example 80 | 81 | To run the example project, clone the repo, and run `DNSPageView.xcodeproj` . 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | ## Usage 96 | 97 | ### 直接使用 DNSPageView 初始化 98 | 99 | ```objective-c 100 | // 创建 DNSPageStyle,设置样式 101 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 102 | style.titleViewScrollEnabled = YES; 103 | style.titleScaleEnabled = YES; 104 | 105 | // 设置标题内容 106 | NSArray *titles = @[@"头条", @"视频", @"娱乐", @"要问", @"体育", @"科技", @"汽车", @"时尚", @"图片", @"游戏", @"房产"]; 107 | 108 | // 创建每一页对应的 controller 109 | for (int i = 0; i < titles.count; i++) { 110 | ContentViewController *controller = [[ContentViewController alloc] init]; 111 | [self addChildViewController:controller]; 112 | } 113 | 114 | CGFloat y = [UIApplication sharedApplication].statusBarFrame.size.height + self.navigationController.navigationBar.frame.size.height; 115 | CGSize size = [UIScreen mainScreen].bounds.size; 116 | 117 | // 创建对应的 DNSPageView,并设置它的 frame 118 | DNSPageView *pageView = [[DNSPageView alloc] initWithFrame:CGRectMake(0, y, size.width, size.height - y) style:style titles:titles childViewControllers:self.childViewControllers currentIndex:7]; 119 | [self.view addSubview:pageView]; 120 | ``` 121 | 122 | 123 | 124 | ### 使用 xib 或者 storyboard 初始化 125 | 126 | 在 `xib` 或者 `storyboard` 中拖出 2 个 `UIView`,让它们分别继承 `DNSPageTitleView` 和 `DNSPageContentView`,拖线到代码中 127 | 128 | ```objective-c 129 | @property (weak, nonatomic) IBOutlet DNSPageTitleView *titleView; 130 | 131 | @property (weak, nonatomic) IBOutlet DNSPageContentView *contentView; 132 | ``` 133 | 134 | 对 DNSPageTitleView 和 DNSPageContentView 进行设置 135 | 136 | ```objective-c 137 | // 创建 DNSPageStyle,设置样式 138 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 139 | style.titleViewBackgroundColor = [UIColor redColor]; 140 | style.showCoverView = YES; 141 | 142 | // 设置标题内容 143 | NSArray *titles = @[@"头条", @"视频", @"娱乐", @"要问", @"体育"]; 144 | 145 | // 设置默认的起始位置 146 | NSInteger startIndex = 2; 147 | 148 | // 创建每一页对应的 controller 149 | for (int i = 0; i < titles.count; i++) { 150 | ContentViewController *controller = [[ContentViewController alloc] init]; 151 | [self addChildViewController:controller]; 152 | } 153 | 154 | // 创建 DNSPageViewManager 来设置它们的样式和布局 155 | DNSPageViewManager *pageViewManager = [[DNSPageViewManager alloc] initWithStyle:style 156 | titles:titles 157 | childViewControllers:self.childViewControllers 158 | currentIndex:currentIndex 159 | titleView:self.titleView 160 | contentView:self.contentView]; 161 | 162 | ``` 163 | 164 | 165 | 166 | ### 使用 DNSPageViewManager 初始化 167 | 168 | 创建 DNSPageViewManager 169 | 170 | ```objective-c 171 | - (DNSPageViewManager *)pageViewManager { 172 | if (!_pageViewManager) { 173 | // 创建 DNSPageStyle,设置样式 174 | DNSPageStyle *style = [[DNSPageStyle alloc] init]; 175 | style.showBottomLine = YES; 176 | style.titleViewScrollEnabled = YES; 177 | style.titleViewBackgroundColor = [UIColor clearColor]; 178 | 179 | // 设置标题内容 180 | NSArray *titles = @[@"头条", @"视频", @"娱乐", @"要问", @"体育"]; 181 | 182 | // 创建每一页对应的 controller 183 | for (int i = 0; i < titles.count; i++) { 184 | ContentViewController *controller = [[ContentViewController alloc] init]; 185 | [self addChildViewController:controller]; 186 | } 187 | _pageViewManager = [[DNSPageViewManager alloc] initWithStyle:style titles:titles childViewControllers:self.childViewControllers]; 188 | } 189 | return _pageViewManager; 190 | } 191 | ``` 192 | 193 | 布局 titleView 和 contentView 194 | 195 | ```objective-c 196 | // 单独设置 titleView 的 frame 197 | self.navigationItem.titleView = self.pageViewManager.titleView; 198 | self.pageViewManager.titleView.frame = CGRectMake(0, 0, 180, 44); 199 | 200 | // 单独设置 contentView 的大小和位置,可以使用 autolayout 或者 frame 201 | DNSPageContentView *contentView = self.pageViewManager.contentView; 202 | [self.view addSubview:contentView]; 203 | [contentView makeConstraints:^(MASConstraintMaker *make) { 204 | make.edges.equalTo(self.view); 205 | }]; 206 | 207 | ``` 208 | 209 | 210 | 211 | ### 样式 212 | 213 | `DNSPageStyle` 中提供了常见样式的属性,可以按照不同的需求进行设置,包括可以设置初始显示的页面 214 | 215 | 216 | 217 | ### 事件回调 218 | 219 | DNSPageView 提供了常见事件监听的代理,它属于 `DNSPageTitleViewDelegate` 的中的属性 220 | 221 | ```objective-c 222 | /** 223 | DNSPageView 的事件回调,如果有需要,请让对应的 childViewController 遵守这个协议 224 | */ 225 | @protocol DNSPageEventHandlerDelegate 226 | @optional 227 | 228 | 229 | /** 230 | 重复点击 pageTitleView 后调用 231 | */ 232 | - (void)titleViewDidSelectSameTitle; 233 | 234 | 235 | /** 236 | pageContentView 的上一页消失的时候,上一页对应的 controller 调用 237 | */ 238 | - (void)contentViewDidDisappear; 239 | 240 | /** 241 | pageContentView 滚动停止的时候,当前页对应的 controller 调用 242 | */ 243 | - (void)contentViewDidEndScroll; 244 | 245 | @end 246 | ``` 247 | 248 | 249 | 250 | ### 常见问题 251 | 252 | - `style.isTitleViewScrollEnabled` 253 | 254 | 如果标签比较少,建议设置 `style.titleViewScrollEnabled = NO`,`titleView` 会固定,`style.titleMargin` 不起作用,每个标签平分整个 `titleView` 的宽度,下划线的宽度等于标签的宽度。 255 | 256 | 如果标签比较多,建议设置 `style.titleViewScrollEnabled = YES`,`titleView` 会滑动,下划线的宽度随着标签文字的宽度变化而变化 257 | 258 | - 标签下划线的宽度跟随文字的宽度 259 | 260 | 设置 `style.titleViewScrollEnabled = YES`,可以参考 demo 中的第四种样式。 261 | 262 | - 由于 `DNSPageView` 是基于 `UIScrollView` 实现,那么就无法避免它的一些特性: 263 | 264 | - 当控制器被 `UINavigationController` 管理,且 `navigationBar.isTranslucent = YES` 的时候,当前控制器的 `view` 是从 `y = 0` 开始布局的,所以为了防止部分内容被 `navigationBar` 遮挡,系统默认会给 `UIScrollView` 添加 offset。如果想取消这个特性: 265 | - iOS 11 以前,在控制器中设置 `self.automaticallyAdjustsScrollViewInsets = NO` 266 | - iOS 11 以后引入 `SafeArea` 概念,设置 `UIScrollView` 的属性 `contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever` 267 | - 其实这个效果还与 `UIViewController` 的其他属性有关系,但因为各种组合的情景过于复杂,所以不在此一一描述 268 | - `DNSPageContentView` 用 `UICollectionView` 实现,所以这个特性有机会造成 `UICollectionView` 的高度小于它的 `item` 的高度,造成奇怪的 Bug。 269 | - 以上只是可能出现的 Bug 之一,由于 `Demo` 不能覆盖所有的场景,不同的布局需求可能会引起不同的 Bug,开发者需要明确了解自己的布局需求,注意细节,了解 iOS 布局特性,并且作出对应的调整,不能完全参照 `Demo`。 270 | 271 | 272 | ## License 273 | 274 | DNSPageView-ObjC is available under the MIT license. See the LICENSE file for more info. 275 | 276 | 277 | -------------------------------------------------------------------------------- /Masonry/MASConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * Enables Constraints to be created with chainable syntax 13 | * Constraint can represent single NSLayoutConstraint (MASViewConstraint) 14 | * or a group of NSLayoutConstraints (MASComposisteConstraint) 15 | */ 16 | @interface MASConstraint : NSObject 17 | 18 | // Chaining Support 19 | 20 | /** 21 | * Modifies the NSLayoutConstraint constant, 22 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 23 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 24 | */ 25 | - (MASConstraint * (^)(MASEdgeInsets insets))insets; 26 | 27 | /** 28 | * Modifies the NSLayoutConstraint constant, 29 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 30 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 31 | */ 32 | - (MASConstraint * (^)(CGFloat inset))inset; 33 | 34 | /** 35 | * Modifies the NSLayoutConstraint constant, 36 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 37 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 38 | */ 39 | - (MASConstraint * (^)(CGSize offset))sizeOffset; 40 | 41 | /** 42 | * Modifies the NSLayoutConstraint constant, 43 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 44 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 45 | */ 46 | - (MASConstraint * (^)(CGPoint offset))centerOffset; 47 | 48 | /** 49 | * Modifies the NSLayoutConstraint constant 50 | */ 51 | - (MASConstraint * (^)(CGFloat offset))offset; 52 | 53 | /** 54 | * Modifies the NSLayoutConstraint constant based on a value type 55 | */ 56 | - (MASConstraint * (^)(NSValue *value))valueOffset; 57 | 58 | /** 59 | * Sets the NSLayoutConstraint multiplier property 60 | */ 61 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy; 62 | 63 | /** 64 | * Sets the NSLayoutConstraint multiplier to 1.0/dividedBy 65 | */ 66 | - (MASConstraint * (^)(CGFloat divider))dividedBy; 67 | 68 | /** 69 | * Sets the NSLayoutConstraint priority to a float or MASLayoutPriority 70 | */ 71 | - (MASConstraint * (^)(MASLayoutPriority priority))priority; 72 | 73 | /** 74 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityLow 75 | */ 76 | - (MASConstraint * (^)(void))priorityLow; 77 | 78 | /** 79 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium 80 | */ 81 | - (MASConstraint * (^)(void))priorityMedium; 82 | 83 | /** 84 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh 85 | */ 86 | - (MASConstraint * (^)(void))priorityHigh; 87 | 88 | /** 89 | * Sets the constraint relation to NSLayoutRelationEqual 90 | * returns a block which accepts one of the following: 91 | * MASViewAttribute, UIView, NSValue, NSArray 92 | * see readme for more details. 93 | */ 94 | - (MASConstraint * (^)(id attr))equalTo; 95 | 96 | /** 97 | * Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual 98 | * returns a block which accepts one of the following: 99 | * MASViewAttribute, UIView, NSValue, NSArray 100 | * see readme for more details. 101 | */ 102 | - (MASConstraint * (^)(id attr))greaterThanOrEqualTo; 103 | 104 | /** 105 | * Sets the constraint relation to NSLayoutRelationLessThanOrEqual 106 | * returns a block which accepts one of the following: 107 | * MASViewAttribute, UIView, NSValue, NSArray 108 | * see readme for more details. 109 | */ 110 | - (MASConstraint * (^)(id attr))lessThanOrEqualTo; 111 | 112 | /** 113 | * Optional semantic property which has no effect but improves the readability of constraint 114 | */ 115 | - (MASConstraint *)with; 116 | 117 | /** 118 | * Optional semantic property which has no effect but improves the readability of constraint 119 | */ 120 | - (MASConstraint *)and; 121 | 122 | /** 123 | * Creates a new MASCompositeConstraint with the called attribute and reciever 124 | */ 125 | - (MASConstraint *)left; 126 | - (MASConstraint *)top; 127 | - (MASConstraint *)right; 128 | - (MASConstraint *)bottom; 129 | - (MASConstraint *)leading; 130 | - (MASConstraint *)trailing; 131 | - (MASConstraint *)width; 132 | - (MASConstraint *)height; 133 | - (MASConstraint *)centerX; 134 | - (MASConstraint *)centerY; 135 | - (MASConstraint *)baseline; 136 | 137 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 138 | 139 | - (MASConstraint *)firstBaseline; 140 | - (MASConstraint *)lastBaseline; 141 | 142 | #endif 143 | 144 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 145 | 146 | - (MASConstraint *)leftMargin; 147 | - (MASConstraint *)rightMargin; 148 | - (MASConstraint *)topMargin; 149 | - (MASConstraint *)bottomMargin; 150 | - (MASConstraint *)leadingMargin; 151 | - (MASConstraint *)trailingMargin; 152 | - (MASConstraint *)centerXWithinMargins; 153 | - (MASConstraint *)centerYWithinMargins; 154 | 155 | #endif 156 | 157 | 158 | /** 159 | * Sets the constraint debug name 160 | */ 161 | - (MASConstraint * (^)(id key))key; 162 | 163 | // NSLayoutConstraint constant Setters 164 | // for use outside of mas_updateConstraints/mas_makeConstraints blocks 165 | 166 | /** 167 | * Modifies the NSLayoutConstraint constant, 168 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 169 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 170 | */ 171 | - (void)setInsets:(MASEdgeInsets)insets; 172 | 173 | /** 174 | * Modifies the NSLayoutConstraint constant, 175 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 176 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 177 | */ 178 | - (void)setInset:(CGFloat)inset; 179 | 180 | /** 181 | * Modifies the NSLayoutConstraint constant, 182 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 183 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 184 | */ 185 | - (void)setSizeOffset:(CGSize)sizeOffset; 186 | 187 | /** 188 | * Modifies the NSLayoutConstraint constant, 189 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 190 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 191 | */ 192 | - (void)setCenterOffset:(CGPoint)centerOffset; 193 | 194 | /** 195 | * Modifies the NSLayoutConstraint constant 196 | */ 197 | - (void)setOffset:(CGFloat)offset; 198 | 199 | 200 | // NSLayoutConstraint Installation support 201 | 202 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 203 | /** 204 | * Whether or not to go through the animator proxy when modifying the constraint 205 | */ 206 | @property (nonatomic, copy, readonly) MASConstraint *animator; 207 | #endif 208 | 209 | /** 210 | * Activates an NSLayoutConstraint if it's supported by an OS. 211 | * Invokes install otherwise. 212 | */ 213 | - (void)activate; 214 | 215 | /** 216 | * Deactivates previously installed/activated NSLayoutConstraint. 217 | */ 218 | - (void)deactivate; 219 | 220 | /** 221 | * Creates a NSLayoutConstraint and adds it to the appropriate view. 222 | */ 223 | - (void)install; 224 | 225 | /** 226 | * Removes previously installed NSLayoutConstraint 227 | */ 228 | - (void)uninstall; 229 | 230 | @end 231 | 232 | 233 | /** 234 | * Convenience auto-boxing macros for MASConstraint methods. 235 | * 236 | * Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax. 237 | * A potential drawback of this is that the unprefixed macros will appear in global scope. 238 | */ 239 | #define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__))) 240 | #define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 241 | #define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 242 | 243 | #define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__))) 244 | 245 | 246 | #ifdef MAS_SHORTHAND_GLOBALS 247 | 248 | #define equalTo(...) mas_equalTo(__VA_ARGS__) 249 | #define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__) 250 | #define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__) 251 | 252 | #define offset(...) mas_offset(__VA_ARGS__) 253 | 254 | #endif 255 | 256 | 257 | @interface MASConstraint (AutoboxingSupport) 258 | 259 | /** 260 | * Aliases to corresponding relation methods (for shorthand macros) 261 | * Also needed to aid autocompletion 262 | */ 263 | - (MASConstraint * (^)(id attr))mas_equalTo; 264 | - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo; 265 | - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo; 266 | 267 | /** 268 | * A dummy method to aid autocompletion 269 | */ 270 | - (MASConstraint * (^)(id offset))mas_offset; 271 | 272 | @end 273 | -------------------------------------------------------------------------------- /Masonry/MASConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.m 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 1/20/14. 6 | // 7 | 8 | #import "MASConstraint.h" 9 | #import "MASConstraint+Private.h" 10 | 11 | #define MASMethodNotImplemented() \ 12 | @throw [NSException exceptionWithName:NSInternalInconsistencyException \ 13 | reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \ 14 | userInfo:nil] 15 | 16 | @implementation MASConstraint 17 | 18 | #pragma mark - Init 19 | 20 | - (id)init { 21 | NSAssert(![self isMemberOfClass:[MASConstraint class]], @"MASConstraint is an abstract class, you should not instantiate it directly."); 22 | return [super init]; 23 | } 24 | 25 | #pragma mark - NSLayoutRelation proxies 26 | 27 | - (MASConstraint * (^)(id))equalTo { 28 | return ^id(id attribute) { 29 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 30 | }; 31 | } 32 | 33 | - (MASConstraint * (^)(id))mas_equalTo { 34 | return ^id(id attribute) { 35 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 36 | }; 37 | } 38 | 39 | - (MASConstraint * (^)(id))greaterThanOrEqualTo { 40 | return ^id(id attribute) { 41 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 42 | }; 43 | } 44 | 45 | - (MASConstraint * (^)(id))mas_greaterThanOrEqualTo { 46 | return ^id(id attribute) { 47 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 48 | }; 49 | } 50 | 51 | - (MASConstraint * (^)(id))lessThanOrEqualTo { 52 | return ^id(id attribute) { 53 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 54 | }; 55 | } 56 | 57 | - (MASConstraint * (^)(id))mas_lessThanOrEqualTo { 58 | return ^id(id attribute) { 59 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 60 | }; 61 | } 62 | 63 | #pragma mark - MASLayoutPriority proxies 64 | 65 | - (MASConstraint * (^)(void))priorityLow { 66 | return ^id{ 67 | self.priority(MASLayoutPriorityDefaultLow); 68 | return self; 69 | }; 70 | } 71 | 72 | - (MASConstraint * (^)(void))priorityMedium { 73 | return ^id{ 74 | self.priority(MASLayoutPriorityDefaultMedium); 75 | return self; 76 | }; 77 | } 78 | 79 | - (MASConstraint * (^)(void))priorityHigh { 80 | return ^id{ 81 | self.priority(MASLayoutPriorityDefaultHigh); 82 | return self; 83 | }; 84 | } 85 | 86 | #pragma mark - NSLayoutConstraint constant proxies 87 | 88 | - (MASConstraint * (^)(MASEdgeInsets))insets { 89 | return ^id(MASEdgeInsets insets){ 90 | self.insets = insets; 91 | return self; 92 | }; 93 | } 94 | 95 | - (MASConstraint * (^)(CGFloat))inset { 96 | return ^id(CGFloat inset){ 97 | self.inset = inset; 98 | return self; 99 | }; 100 | } 101 | 102 | - (MASConstraint * (^)(CGSize))sizeOffset { 103 | return ^id(CGSize offset) { 104 | self.sizeOffset = offset; 105 | return self; 106 | }; 107 | } 108 | 109 | - (MASConstraint * (^)(CGPoint))centerOffset { 110 | return ^id(CGPoint offset) { 111 | self.centerOffset = offset; 112 | return self; 113 | }; 114 | } 115 | 116 | - (MASConstraint * (^)(CGFloat))offset { 117 | return ^id(CGFloat offset){ 118 | self.offset = offset; 119 | return self; 120 | }; 121 | } 122 | 123 | - (MASConstraint * (^)(NSValue *value))valueOffset { 124 | return ^id(NSValue *offset) { 125 | NSAssert([offset isKindOfClass:NSValue.class], @"expected an NSValue offset, got: %@", offset); 126 | [self setLayoutConstantWithValue:offset]; 127 | return self; 128 | }; 129 | } 130 | 131 | - (MASConstraint * (^)(id offset))mas_offset { 132 | // Will never be called due to macro 133 | return nil; 134 | } 135 | 136 | #pragma mark - NSLayoutConstraint constant setter 137 | 138 | - (void)setLayoutConstantWithValue:(NSValue *)value { 139 | if ([value isKindOfClass:NSNumber.class]) { 140 | self.offset = [(NSNumber *)value doubleValue]; 141 | } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) { 142 | CGPoint point; 143 | [value getValue:&point]; 144 | self.centerOffset = point; 145 | } else if (strcmp(value.objCType, @encode(CGSize)) == 0) { 146 | CGSize size; 147 | [value getValue:&size]; 148 | self.sizeOffset = size; 149 | } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) { 150 | MASEdgeInsets insets; 151 | [value getValue:&insets]; 152 | self.insets = insets; 153 | } else { 154 | NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value); 155 | } 156 | } 157 | 158 | #pragma mark - Semantic properties 159 | 160 | - (MASConstraint *)with { 161 | return self; 162 | } 163 | 164 | - (MASConstraint *)and { 165 | return self; 166 | } 167 | 168 | #pragma mark - Chaining 169 | 170 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute __unused)layoutAttribute { 171 | MASMethodNotImplemented(); 172 | } 173 | 174 | - (MASConstraint *)left { 175 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 176 | } 177 | 178 | - (MASConstraint *)top { 179 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 180 | } 181 | 182 | - (MASConstraint *)right { 183 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 184 | } 185 | 186 | - (MASConstraint *)bottom { 187 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 188 | } 189 | 190 | - (MASConstraint *)leading { 191 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 192 | } 193 | 194 | - (MASConstraint *)trailing { 195 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 196 | } 197 | 198 | - (MASConstraint *)width { 199 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 200 | } 201 | 202 | - (MASConstraint *)height { 203 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 204 | } 205 | 206 | - (MASConstraint *)centerX { 207 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 208 | } 209 | 210 | - (MASConstraint *)centerY { 211 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 212 | } 213 | 214 | - (MASConstraint *)baseline { 215 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 216 | } 217 | 218 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 219 | 220 | - (MASConstraint *)firstBaseline { 221 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 222 | } 223 | - (MASConstraint *)lastBaseline { 224 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 225 | } 226 | 227 | #endif 228 | 229 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 230 | 231 | - (MASConstraint *)leftMargin { 232 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 233 | } 234 | 235 | - (MASConstraint *)rightMargin { 236 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 237 | } 238 | 239 | - (MASConstraint *)topMargin { 240 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 241 | } 242 | 243 | - (MASConstraint *)bottomMargin { 244 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 245 | } 246 | 247 | - (MASConstraint *)leadingMargin { 248 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 249 | } 250 | 251 | - (MASConstraint *)trailingMargin { 252 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 253 | } 254 | 255 | - (MASConstraint *)centerXWithinMargins { 256 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 257 | } 258 | 259 | - (MASConstraint *)centerYWithinMargins { 260 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 261 | } 262 | 263 | #endif 264 | 265 | #pragma mark - Abstract 266 | 267 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); } 268 | 269 | - (MASConstraint * (^)(CGFloat divider))dividedBy { MASMethodNotImplemented(); } 270 | 271 | - (MASConstraint * (^)(MASLayoutPriority priority))priority { MASMethodNotImplemented(); } 272 | 273 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { MASMethodNotImplemented(); } 274 | 275 | - (MASConstraint * (^)(id key))key { MASMethodNotImplemented(); } 276 | 277 | - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); } 278 | 279 | - (void)setInset:(CGFloat __unused)inset { MASMethodNotImplemented(); } 280 | 281 | - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); } 282 | 283 | - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); } 284 | 285 | - (void)setOffset:(CGFloat __unused)offset { MASMethodNotImplemented(); } 286 | 287 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 288 | 289 | - (MASConstraint *)animator { MASMethodNotImplemented(); } 290 | 291 | #endif 292 | 293 | - (void)activate { MASMethodNotImplemented(); } 294 | 295 | - (void)deactivate { MASMethodNotImplemented(); } 296 | 297 | - (void)install { MASMethodNotImplemented(); } 298 | 299 | - (void)uninstall { MASMethodNotImplemented(); } 300 | 301 | @end 302 | -------------------------------------------------------------------------------- /Masonry/MASConstraintMaker.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintMaker.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraintMaker.h" 10 | #import "MASViewConstraint.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASConstraint+Private.h" 13 | #import "MASViewAttribute.h" 14 | #import "View+MASAdditions.h" 15 | 16 | @interface MASConstraintMaker () 17 | 18 | @property (nonatomic, weak) MAS_VIEW *view; 19 | @property (nonatomic, strong) NSMutableArray *constraints; 20 | 21 | @end 22 | 23 | @implementation MASConstraintMaker 24 | 25 | - (id)initWithView:(MAS_VIEW *)view { 26 | self = [super init]; 27 | if (!self) return nil; 28 | 29 | self.view = view; 30 | self.constraints = NSMutableArray.new; 31 | 32 | return self; 33 | } 34 | 35 | - (NSArray *)install { 36 | if (self.removeExisting) { 37 | NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view]; 38 | for (MASConstraint *constraint in installedConstraints) { 39 | [constraint uninstall]; 40 | } 41 | } 42 | NSArray *constraints = self.constraints.copy; 43 | for (MASConstraint *constraint in constraints) { 44 | constraint.updateExisting = self.updateExisting; 45 | [constraint install]; 46 | } 47 | [self.constraints removeAllObjects]; 48 | return constraints; 49 | } 50 | 51 | #pragma mark - MASConstraintDelegate 52 | 53 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 54 | NSUInteger index = [self.constraints indexOfObject:constraint]; 55 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 56 | [self.constraints replaceObjectAtIndex:index withObject:replacementConstraint]; 57 | } 58 | 59 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 60 | MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute]; 61 | MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute]; 62 | if ([constraint isKindOfClass:MASViewConstraint.class]) { 63 | //replace with composite constraint 64 | NSArray *children = @[constraint, newConstraint]; 65 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 66 | compositeConstraint.delegate = self; 67 | [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint]; 68 | return compositeConstraint; 69 | } 70 | if (!constraint) { 71 | newConstraint.delegate = self; 72 | [self.constraints addObject:newConstraint]; 73 | } 74 | return newConstraint; 75 | } 76 | 77 | - (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs { 78 | __unused MASAttribute anyAttribute = (MASAttributeLeft | MASAttributeRight | MASAttributeTop | MASAttributeBottom | MASAttributeLeading 79 | | MASAttributeTrailing | MASAttributeWidth | MASAttributeHeight | MASAttributeCenterX 80 | | MASAttributeCenterY | MASAttributeBaseline 81 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 82 | | MASAttributeFirstBaseline | MASAttributeLastBaseline 83 | #endif 84 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 85 | | MASAttributeLeftMargin | MASAttributeRightMargin | MASAttributeTopMargin | MASAttributeBottomMargin 86 | | MASAttributeLeadingMargin | MASAttributeTrailingMargin | MASAttributeCenterXWithinMargins 87 | | MASAttributeCenterYWithinMargins 88 | #endif 89 | ); 90 | 91 | NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)"); 92 | 93 | NSMutableArray *attributes = [NSMutableArray array]; 94 | 95 | if (attrs & MASAttributeLeft) [attributes addObject:self.view.mas_left]; 96 | if (attrs & MASAttributeRight) [attributes addObject:self.view.mas_right]; 97 | if (attrs & MASAttributeTop) [attributes addObject:self.view.mas_top]; 98 | if (attrs & MASAttributeBottom) [attributes addObject:self.view.mas_bottom]; 99 | if (attrs & MASAttributeLeading) [attributes addObject:self.view.mas_leading]; 100 | if (attrs & MASAttributeTrailing) [attributes addObject:self.view.mas_trailing]; 101 | if (attrs & MASAttributeWidth) [attributes addObject:self.view.mas_width]; 102 | if (attrs & MASAttributeHeight) [attributes addObject:self.view.mas_height]; 103 | if (attrs & MASAttributeCenterX) [attributes addObject:self.view.mas_centerX]; 104 | if (attrs & MASAttributeCenterY) [attributes addObject:self.view.mas_centerY]; 105 | if (attrs & MASAttributeBaseline) [attributes addObject:self.view.mas_baseline]; 106 | 107 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 108 | 109 | if (attrs & MASAttributeFirstBaseline) [attributes addObject:self.view.mas_firstBaseline]; 110 | if (attrs & MASAttributeLastBaseline) [attributes addObject:self.view.mas_lastBaseline]; 111 | 112 | #endif 113 | 114 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 115 | 116 | if (attrs & MASAttributeLeftMargin) [attributes addObject:self.view.mas_leftMargin]; 117 | if (attrs & MASAttributeRightMargin) [attributes addObject:self.view.mas_rightMargin]; 118 | if (attrs & MASAttributeTopMargin) [attributes addObject:self.view.mas_topMargin]; 119 | if (attrs & MASAttributeBottomMargin) [attributes addObject:self.view.mas_bottomMargin]; 120 | if (attrs & MASAttributeLeadingMargin) [attributes addObject:self.view.mas_leadingMargin]; 121 | if (attrs & MASAttributeTrailingMargin) [attributes addObject:self.view.mas_trailingMargin]; 122 | if (attrs & MASAttributeCenterXWithinMargins) [attributes addObject:self.view.mas_centerXWithinMargins]; 123 | if (attrs & MASAttributeCenterYWithinMargins) [attributes addObject:self.view.mas_centerYWithinMargins]; 124 | 125 | #endif 126 | 127 | NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count]; 128 | 129 | for (MASViewAttribute *a in attributes) { 130 | [children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]]; 131 | } 132 | 133 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 134 | constraint.delegate = self; 135 | [self.constraints addObject:constraint]; 136 | return constraint; 137 | } 138 | 139 | #pragma mark - standard Attributes 140 | 141 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 142 | return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute]; 143 | } 144 | 145 | - (MASConstraint *)left { 146 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 147 | } 148 | 149 | - (MASConstraint *)top { 150 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 151 | } 152 | 153 | - (MASConstraint *)right { 154 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 155 | } 156 | 157 | - (MASConstraint *)bottom { 158 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 159 | } 160 | 161 | - (MASConstraint *)leading { 162 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 163 | } 164 | 165 | - (MASConstraint *)trailing { 166 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 167 | } 168 | 169 | - (MASConstraint *)width { 170 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 171 | } 172 | 173 | - (MASConstraint *)height { 174 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 175 | } 176 | 177 | - (MASConstraint *)centerX { 178 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 179 | } 180 | 181 | - (MASConstraint *)centerY { 182 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 183 | } 184 | 185 | - (MASConstraint *)baseline { 186 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 187 | } 188 | 189 | - (MASConstraint *(^)(MASAttribute))attributes { 190 | return ^(MASAttribute attrs){ 191 | return [self addConstraintWithAttributes:attrs]; 192 | }; 193 | } 194 | 195 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 196 | 197 | - (MASConstraint *)firstBaseline { 198 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 199 | } 200 | 201 | - (MASConstraint *)lastBaseline { 202 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 203 | } 204 | 205 | #endif 206 | 207 | 208 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) 209 | 210 | - (MASConstraint *)leftMargin { 211 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 212 | } 213 | 214 | - (MASConstraint *)rightMargin { 215 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 216 | } 217 | 218 | - (MASConstraint *)topMargin { 219 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 220 | } 221 | 222 | - (MASConstraint *)bottomMargin { 223 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 224 | } 225 | 226 | - (MASConstraint *)leadingMargin { 227 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 228 | } 229 | 230 | - (MASConstraint *)trailingMargin { 231 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 232 | } 233 | 234 | - (MASConstraint *)centerXWithinMargins { 235 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 236 | } 237 | 238 | - (MASConstraint *)centerYWithinMargins { 239 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 240 | } 241 | 242 | #endif 243 | 244 | 245 | #pragma mark - composite Attributes 246 | 247 | - (MASConstraint *)edges { 248 | return [self addConstraintWithAttributes:MASAttributeTop | MASAttributeLeft | MASAttributeRight | MASAttributeBottom]; 249 | } 250 | 251 | - (MASConstraint *)size { 252 | return [self addConstraintWithAttributes:MASAttributeWidth | MASAttributeHeight]; 253 | } 254 | 255 | - (MASConstraint *)center { 256 | return [self addConstraintWithAttributes:MASAttributeCenterX | MASAttributeCenterY]; 257 | } 258 | 259 | #pragma mark - grouping 260 | 261 | - (MASConstraint *(^)(dispatch_block_t group))group { 262 | return ^id(dispatch_block_t group) { 263 | NSInteger previousCount = self.constraints.count; 264 | group(); 265 | 266 | NSArray *children = [self.constraints subarrayWithRange:NSMakeRange(previousCount, self.constraints.count - previousCount)]; 267 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 268 | constraint.delegate = self; 269 | return constraint; 270 | }; 271 | } 272 | 273 | @end 274 | -------------------------------------------------------------------------------- /DNSPageView-ObjC/DNSPageContentView.m: -------------------------------------------------------------------------------- 1 | // 2 | // DNSPageContentView.m 3 | // DNSPageView-ObjC 4 | // 5 | // Created by Daniels on 2018/9/24. 6 | // Copyright © 2018年 Daniels. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "DNSPageContentView.h" 28 | #import "DNSPageCollectionViewFlowLayout.h" 29 | #import "DNSPageTitleView.h" 30 | 31 | 32 | @interface DNSPageContentView () 33 | 34 | @property (nonatomic, assign) CGFloat startOffsetX; 35 | 36 | @property (nonatomic, assign, getter=isForbidDelegate) BOOL forbidDelegate; 37 | 38 | @property (nonatomic, strong) DNSPageStyle *style; 39 | 40 | @property (nonatomic, strong,) NSArray *childViewControllers; 41 | 42 | @property (nonatomic, assign) NSInteger currentIndex; 43 | 44 | @property (nonatomic, strong) UICollectionView *collectionView; 45 | 46 | @end 47 | 48 | @implementation DNSPageContentView 49 | 50 | static NSString *const reuseIdentifier = @"reuseIdentifier"; 51 | 52 | - (UICollectionView *)collectionView { 53 | if (!_collectionView) { 54 | DNSPageCollectionViewFlowLayout *layout = [[DNSPageCollectionViewFlowLayout alloc] init]; 55 | layout.minimumInteritemSpacing = 0; 56 | layout.minimumLineSpacing = 0; 57 | layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; 58 | 59 | _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; 60 | _collectionView.showsHorizontalScrollIndicator = NO; 61 | _collectionView.pagingEnabled = YES; 62 | _collectionView.scrollsToTop = NO; 63 | _collectionView.dataSource = self; 64 | _collectionView.delegate = self; 65 | _collectionView.bounces = NO; 66 | if (@available(iOS 10, *)) { 67 | _collectionView.prefetchingEnabled = NO; 68 | } 69 | [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 70 | } 71 | return _collectionView; 72 | } 73 | 74 | - (DNSPageStyle *)style { 75 | if (!_style) { 76 | _style = [[DNSPageStyle alloc] init]; 77 | } 78 | return _style; 79 | } 80 | 81 | - (NSArray *)childViewControllers { 82 | if (!_childViewControllers) { 83 | _childViewControllers = [NSArray array]; 84 | } 85 | return _childViewControllers; 86 | } 87 | 88 | - (void)setCurrentIndex:(NSInteger)currentIndex { 89 | _currentIndex = currentIndex; 90 | if (!self.delegate && [self.container respondsToSelector:@selector(updateCurrentIndex:)]) { 91 | [self.container updateCurrentIndex:currentIndex]; 92 | } 93 | } 94 | 95 | - (instancetype)initWithFrame:(CGRect)frame 96 | style:(DNSPageStyle *)style 97 | childViewControllers:(NSArray *)childViewControllers 98 | currentIndex:(NSInteger)currentIndex { 99 | NSParameterAssert(currentIndex >= 0 && currentIndex < childViewControllers.count); 100 | self = [super initWithFrame:frame]; 101 | if (self) { 102 | [self addSubview:self.collectionView]; 103 | [self configureWithChildViewControllers:childViewControllers style:style currentIndex:currentIndex]; 104 | } 105 | return self; 106 | } 107 | 108 | - (instancetype)initWithFrame:(CGRect)frame 109 | style:(DNSPageStyle *)style 110 | childViewControllers:(NSArray *)childViewControllers { 111 | return [self initWithFrame:frame style:style childViewControllers:childViewControllers currentIndex:0]; 112 | } 113 | 114 | - (instancetype)initWithCoder:(NSCoder *)coder { 115 | self = [super initWithCoder:coder]; 116 | if (self) { 117 | [self addSubview:self.collectionView]; 118 | } 119 | return self; 120 | } 121 | 122 | 123 | - (void)layoutSubviews { 124 | [super layoutSubviews]; 125 | self.collectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); 126 | DNSPageCollectionViewFlowLayout *layout = (DNSPageCollectionViewFlowLayout *)self.collectionView.collectionViewLayout; 127 | layout.itemSize = self.frame.size; 128 | layout.offset = self.currentIndex * self.frame.size.width; 129 | } 130 | 131 | - (void)configureWithChildViewControllers:(NSArray *)childViewControllers 132 | style:(DNSPageStyle *)style 133 | currentIndex:(NSInteger)currentIndex { 134 | if (childViewControllers) { 135 | self.childViewControllers = childViewControllers; 136 | } 137 | if (style) { 138 | self.style = style; 139 | } 140 | if (currentIndex >= 0) { 141 | [self.collectionView.collectionViewLayout invalidateLayout]; 142 | self.currentIndex = currentIndex; 143 | } 144 | [self configureSubViews]; 145 | [self.collectionView reloadData]; 146 | [self setNeedsLayout]; 147 | } 148 | 149 | - (void)configureWithChildViewControllers:(NSArray *)childViewControllers 150 | style:(DNSPageStyle *)style { 151 | [self configureWithChildViewControllers:childViewControllers style:style currentIndex:-1]; 152 | } 153 | 154 | - (void)configureSubViews { 155 | self.collectionView.backgroundColor = self.style.contentViewBackgroundColor; 156 | self.collectionView.scrollEnabled = self.style.isContentScrollEnabled; 157 | } 158 | 159 | 160 | #pragma mark - UICollectionViewDelegate & UICollectionViewDataSource 161 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 162 | return self.childViewControllers.count; 163 | } 164 | 165 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 166 | UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 167 | for (UIView *subview in cell.contentView.subviews) { 168 | [subview removeFromSuperview]; 169 | } 170 | UIViewController *childViewController = self.childViewControllers[indexPath.item]; 171 | if ([childViewController conformsToProtocol:@protocol(DNSPageEventHandlerDelegate)]) { 172 | self.eventHandler = (UIViewController *)childViewController; 173 | } 174 | childViewController.view.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height); 175 | [cell.contentView addSubview:childViewController.view]; 176 | 177 | return cell; 178 | } 179 | 180 | - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 181 | self.forbidDelegate = NO; 182 | self.startOffsetX = scrollView.contentOffset.x; 183 | } 184 | 185 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 186 | [self updateUI:scrollView]; 187 | } 188 | 189 | - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { 190 | if (!decelerate) { 191 | [self collectionViewDidEndScroll:scrollView]; 192 | } 193 | } 194 | 195 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 196 | [self collectionViewDidEndScroll:scrollView]; 197 | } 198 | 199 | 200 | - (void)collectionViewDidEndScroll:(UIScrollView *)scrollView { 201 | NSInteger index = (NSInteger)round(scrollView.contentOffset.x / scrollView.frame.size.width); 202 | 203 | if ([self.delegate respondsToSelector:@selector(contentView:didEndScrollAtIndex:)]) { 204 | [self.delegate contentView:self didEndScrollAtIndex:index]; 205 | } 206 | 207 | if (index != self.currentIndex) { 208 | UIViewController *childViewController = self.childViewControllers[self.currentIndex]; 209 | if ([childViewController conformsToProtocol:@protocol(DNSPageEventHandlerDelegate)]) { 210 | UIViewController *eventHandler = (UIViewController *)childViewController; 211 | if ([eventHandler respondsToSelector:@selector(contentViewDidDisappear)]) { 212 | [eventHandler contentViewDidDisappear]; 213 | } 214 | } 215 | } 216 | 217 | self.currentIndex = index; 218 | 219 | UIViewController *childViewController = self.childViewControllers[index]; 220 | if ([childViewController conformsToProtocol:@protocol(DNSPageEventHandlerDelegate)]) { 221 | self.eventHandler = (UIViewController *)childViewController; 222 | if ([self.eventHandler respondsToSelector:@selector(contentViewDidEndScroll)]) { 223 | [self.eventHandler contentViewDidEndScroll]; 224 | } 225 | } 226 | 227 | 228 | } 229 | 230 | - (void)updateUI:(UIScrollView *)scrollView { 231 | if (self.isForbidDelegate) { 232 | return; 233 | } 234 | CGFloat progress = 0; 235 | NSInteger targetIndex = 0; 236 | NSInteger sourceIndex = 0; 237 | progress = (NSInteger)scrollView.contentOffset.x % (NSInteger)scrollView.frame.size.width / scrollView.frame.size.width; 238 | if (progress == 0 || isnan(progress)) { 239 | return; 240 | } 241 | 242 | NSInteger index = (NSInteger)(scrollView.contentOffset.x / scrollView.frame.size.width); 243 | 244 | if (self.collectionView.contentOffset.x > self.startOffsetX) { 245 | sourceIndex = index; 246 | targetIndex = index + 1; 247 | if (targetIndex >= self.childViewControllers.count) { 248 | return; 249 | } 250 | } else { 251 | sourceIndex = index + 1; 252 | targetIndex = index; 253 | progress = 1 - progress; 254 | if (targetIndex < 0) { 255 | return; 256 | } 257 | } 258 | if (progress > 0.998) { 259 | progress = 1; 260 | } 261 | if ([self.delegate respondsToSelector:@selector(contentView:scrollingWithSourceIndex:targetIndex:progress:)]) { 262 | [self.delegate contentView:self scrollingWithSourceIndex:sourceIndex targetIndex:targetIndex progress:progress]; 263 | } 264 | } 265 | 266 | #pragma mark - DNSPageTitleViewDelegate 267 | - (void)titleView:(DNSPageTitleView *)titleView didSelectAtIndex:(NSInteger)index { 268 | self.forbidDelegate = YES; 269 | 270 | if (index >= self.childViewControllers.count) { 271 | return; 272 | } 273 | self.currentIndex = index; 274 | NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0]; 275 | [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; 276 | } 277 | 278 | 279 | 280 | @end 281 | -------------------------------------------------------------------------------- /Masonry/MASViewConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASViewConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASLayoutConstraint.h" 13 | #import "View+MASAdditions.h" 14 | #import 15 | 16 | @interface MAS_VIEW (MASConstraints) 17 | 18 | @property (nonatomic, readonly) NSMutableSet *mas_installedConstraints; 19 | 20 | @end 21 | 22 | @implementation MAS_VIEW (MASConstraints) 23 | 24 | static char kInstalledConstraintsKey; 25 | 26 | - (NSMutableSet *)mas_installedConstraints { 27 | NSMutableSet *constraints = objc_getAssociatedObject(self, &kInstalledConstraintsKey); 28 | if (!constraints) { 29 | constraints = [NSMutableSet set]; 30 | objc_setAssociatedObject(self, &kInstalledConstraintsKey, constraints, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 31 | } 32 | return constraints; 33 | } 34 | 35 | @end 36 | 37 | 38 | @interface MASViewConstraint () 39 | 40 | @property (nonatomic, strong, readwrite) MASViewAttribute *secondViewAttribute; 41 | @property (nonatomic, weak) MAS_VIEW *installedView; 42 | @property (nonatomic, weak) MASLayoutConstraint *layoutConstraint; 43 | @property (nonatomic, assign) NSLayoutRelation layoutRelation; 44 | @property (nonatomic, assign) MASLayoutPriority layoutPriority; 45 | @property (nonatomic, assign) CGFloat layoutMultiplier; 46 | @property (nonatomic, assign) CGFloat layoutConstant; 47 | @property (nonatomic, assign) BOOL hasLayoutRelation; 48 | @property (nonatomic, strong) id mas_key; 49 | @property (nonatomic, assign) BOOL useAnimator; 50 | 51 | @end 52 | 53 | @implementation MASViewConstraint 54 | 55 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute { 56 | self = [super init]; 57 | if (!self) return nil; 58 | 59 | _firstViewAttribute = firstViewAttribute; 60 | self.layoutPriority = MASLayoutPriorityRequired; 61 | self.layoutMultiplier = 1; 62 | 63 | return self; 64 | } 65 | 66 | #pragma mark - NSCoping 67 | 68 | - (id)copyWithZone:(NSZone __unused *)zone { 69 | MASViewConstraint *constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:self.firstViewAttribute]; 70 | constraint.layoutConstant = self.layoutConstant; 71 | constraint.layoutRelation = self.layoutRelation; 72 | constraint.layoutPriority = self.layoutPriority; 73 | constraint.layoutMultiplier = self.layoutMultiplier; 74 | constraint.delegate = self.delegate; 75 | return constraint; 76 | } 77 | 78 | #pragma mark - Public 79 | 80 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view { 81 | return [view.mas_installedConstraints allObjects]; 82 | } 83 | 84 | #pragma mark - Private 85 | 86 | - (void)setLayoutConstant:(CGFloat)layoutConstant { 87 | _layoutConstant = layoutConstant; 88 | 89 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 90 | if (self.useAnimator) { 91 | [self.layoutConstraint.animator setConstant:layoutConstant]; 92 | } else { 93 | self.layoutConstraint.constant = layoutConstant; 94 | } 95 | #else 96 | self.layoutConstraint.constant = layoutConstant; 97 | #endif 98 | } 99 | 100 | - (void)setLayoutRelation:(NSLayoutRelation)layoutRelation { 101 | _layoutRelation = layoutRelation; 102 | self.hasLayoutRelation = YES; 103 | } 104 | 105 | - (BOOL)supportsActiveProperty { 106 | return [self.layoutConstraint respondsToSelector:@selector(isActive)]; 107 | } 108 | 109 | - (BOOL)isActive { 110 | BOOL active = YES; 111 | if ([self supportsActiveProperty]) { 112 | active = [self.layoutConstraint isActive]; 113 | } 114 | 115 | return active; 116 | } 117 | 118 | - (BOOL)hasBeenInstalled { 119 | return (self.layoutConstraint != nil) && [self isActive]; 120 | } 121 | 122 | - (void)setSecondViewAttribute:(id)secondViewAttribute { 123 | if ([secondViewAttribute isKindOfClass:NSValue.class]) { 124 | [self setLayoutConstantWithValue:secondViewAttribute]; 125 | } else if ([secondViewAttribute isKindOfClass:MAS_VIEW.class]) { 126 | _secondViewAttribute = [[MASViewAttribute alloc] initWithView:secondViewAttribute layoutAttribute:self.firstViewAttribute.layoutAttribute]; 127 | } else if ([secondViewAttribute isKindOfClass:MASViewAttribute.class]) { 128 | _secondViewAttribute = secondViewAttribute; 129 | } else { 130 | NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute); 131 | } 132 | } 133 | 134 | #pragma mark - NSLayoutConstraint multiplier proxies 135 | 136 | - (MASConstraint * (^)(CGFloat))multipliedBy { 137 | return ^id(CGFloat multiplier) { 138 | NSAssert(!self.hasBeenInstalled, 139 | @"Cannot modify constraint multiplier after it has been installed"); 140 | 141 | self.layoutMultiplier = multiplier; 142 | return self; 143 | }; 144 | } 145 | 146 | 147 | - (MASConstraint * (^)(CGFloat))dividedBy { 148 | return ^id(CGFloat divider) { 149 | NSAssert(!self.hasBeenInstalled, 150 | @"Cannot modify constraint multiplier after it has been installed"); 151 | 152 | self.layoutMultiplier = 1.0/divider; 153 | return self; 154 | }; 155 | } 156 | 157 | #pragma mark - MASLayoutPriority proxy 158 | 159 | - (MASConstraint * (^)(MASLayoutPriority))priority { 160 | return ^id(MASLayoutPriority priority) { 161 | NSAssert(!self.hasBeenInstalled, 162 | @"Cannot modify constraint priority after it has been installed"); 163 | 164 | self.layoutPriority = priority; 165 | return self; 166 | }; 167 | } 168 | 169 | #pragma mark - NSLayoutRelation proxy 170 | 171 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 172 | return ^id(id attribute, NSLayoutRelation relation) { 173 | if ([attribute isKindOfClass:NSArray.class]) { 174 | NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation"); 175 | NSMutableArray *children = NSMutableArray.new; 176 | for (id attr in attribute) { 177 | MASViewConstraint *viewConstraint = [self copy]; 178 | viewConstraint.layoutRelation = relation; 179 | viewConstraint.secondViewAttribute = attr; 180 | [children addObject:viewConstraint]; 181 | } 182 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 183 | compositeConstraint.delegate = self.delegate; 184 | [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint]; 185 | return compositeConstraint; 186 | } else { 187 | NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation"); 188 | self.layoutRelation = relation; 189 | self.secondViewAttribute = attribute; 190 | return self; 191 | } 192 | }; 193 | } 194 | 195 | #pragma mark - Semantic properties 196 | 197 | - (MASConstraint *)with { 198 | return self; 199 | } 200 | 201 | - (MASConstraint *)and { 202 | return self; 203 | } 204 | 205 | #pragma mark - attribute chaining 206 | 207 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 208 | NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation"); 209 | 210 | return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 211 | } 212 | 213 | #pragma mark - Animator proxy 214 | 215 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 216 | 217 | - (MASConstraint *)animator { 218 | self.useAnimator = YES; 219 | return self; 220 | } 221 | 222 | #endif 223 | 224 | #pragma mark - debug helpers 225 | 226 | - (MASConstraint * (^)(id))key { 227 | return ^id(id key) { 228 | self.mas_key = key; 229 | return self; 230 | }; 231 | } 232 | 233 | #pragma mark - NSLayoutConstraint constant setters 234 | 235 | - (void)setInsets:(MASEdgeInsets)insets { 236 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 237 | switch (layoutAttribute) { 238 | case NSLayoutAttributeLeft: 239 | case NSLayoutAttributeLeading: 240 | self.layoutConstant = insets.left; 241 | break; 242 | case NSLayoutAttributeTop: 243 | self.layoutConstant = insets.top; 244 | break; 245 | case NSLayoutAttributeBottom: 246 | self.layoutConstant = -insets.bottom; 247 | break; 248 | case NSLayoutAttributeRight: 249 | case NSLayoutAttributeTrailing: 250 | self.layoutConstant = -insets.right; 251 | break; 252 | default: 253 | break; 254 | } 255 | } 256 | 257 | - (void)setInset:(CGFloat)inset { 258 | [self setInsets:(MASEdgeInsets){.top = inset, .left = inset, .bottom = inset, .right = inset}]; 259 | } 260 | 261 | - (void)setOffset:(CGFloat)offset { 262 | self.layoutConstant = offset; 263 | } 264 | 265 | - (void)setSizeOffset:(CGSize)sizeOffset { 266 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 267 | switch (layoutAttribute) { 268 | case NSLayoutAttributeWidth: 269 | self.layoutConstant = sizeOffset.width; 270 | break; 271 | case NSLayoutAttributeHeight: 272 | self.layoutConstant = sizeOffset.height; 273 | break; 274 | default: 275 | break; 276 | } 277 | } 278 | 279 | - (void)setCenterOffset:(CGPoint)centerOffset { 280 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 281 | switch (layoutAttribute) { 282 | case NSLayoutAttributeCenterX: 283 | self.layoutConstant = centerOffset.x; 284 | break; 285 | case NSLayoutAttributeCenterY: 286 | self.layoutConstant = centerOffset.y; 287 | break; 288 | default: 289 | break; 290 | } 291 | } 292 | 293 | #pragma mark - MASConstraint 294 | 295 | - (void)activate { 296 | [self install]; 297 | } 298 | 299 | - (void)deactivate { 300 | [self uninstall]; 301 | } 302 | 303 | - (void)install { 304 | if (self.hasBeenInstalled) { 305 | return; 306 | } 307 | 308 | if ([self supportsActiveProperty] && self.layoutConstraint) { 309 | self.layoutConstraint.active = YES; 310 | [self.firstViewAttribute.view.mas_installedConstraints addObject:self]; 311 | return; 312 | } 313 | 314 | MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item; 315 | NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute; 316 | MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item; 317 | NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute; 318 | 319 | // alignment attributes must have a secondViewAttribute 320 | // therefore we assume that is refering to superview 321 | // eg make.left.equalTo(@10) 322 | if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) { 323 | secondLayoutItem = self.firstViewAttribute.view.superview; 324 | secondLayoutAttribute = firstLayoutAttribute; 325 | } 326 | 327 | MASLayoutConstraint *layoutConstraint 328 | = [MASLayoutConstraint constraintWithItem:firstLayoutItem 329 | attribute:firstLayoutAttribute 330 | relatedBy:self.layoutRelation 331 | toItem:secondLayoutItem 332 | attribute:secondLayoutAttribute 333 | multiplier:self.layoutMultiplier 334 | constant:self.layoutConstant]; 335 | 336 | layoutConstraint.priority = self.layoutPriority; 337 | layoutConstraint.mas_key = self.mas_key; 338 | 339 | if (self.secondViewAttribute.view) { 340 | MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view]; 341 | NSAssert(closestCommonSuperview, 342 | @"couldn't find a common superview for %@ and %@", 343 | self.firstViewAttribute.view, self.secondViewAttribute.view); 344 | self.installedView = closestCommonSuperview; 345 | } else if (self.firstViewAttribute.isSizeAttribute) { 346 | self.installedView = self.firstViewAttribute.view; 347 | } else { 348 | self.installedView = self.firstViewAttribute.view.superview; 349 | } 350 | 351 | 352 | MASLayoutConstraint *existingConstraint = nil; 353 | if (self.updateExisting) { 354 | existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint]; 355 | } 356 | if (existingConstraint) { 357 | // just update the constant 358 | existingConstraint.constant = layoutConstraint.constant; 359 | self.layoutConstraint = existingConstraint; 360 | } else { 361 | [self.installedView addConstraint:layoutConstraint]; 362 | self.layoutConstraint = layoutConstraint; 363 | [firstLayoutItem.mas_installedConstraints addObject:self]; 364 | } 365 | } 366 | 367 | - (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layoutConstraint { 368 | // check if any constraints are the same apart from the only mutable property constant 369 | 370 | // go through constraints in reverse as we do not want to match auto-resizing or interface builder constraints 371 | // and they are likely to be added first. 372 | for (NSLayoutConstraint *existingConstraint in self.installedView.constraints.reverseObjectEnumerator) { 373 | if (![existingConstraint isKindOfClass:MASLayoutConstraint.class]) continue; 374 | if (existingConstraint.firstItem != layoutConstraint.firstItem) continue; 375 | if (existingConstraint.secondItem != layoutConstraint.secondItem) continue; 376 | if (existingConstraint.firstAttribute != layoutConstraint.firstAttribute) continue; 377 | if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue; 378 | if (existingConstraint.relation != layoutConstraint.relation) continue; 379 | if (existingConstraint.multiplier != layoutConstraint.multiplier) continue; 380 | if (existingConstraint.priority != layoutConstraint.priority) continue; 381 | 382 | return (id)existingConstraint; 383 | } 384 | return nil; 385 | } 386 | 387 | - (void)uninstall { 388 | if ([self supportsActiveProperty]) { 389 | self.layoutConstraint.active = NO; 390 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 391 | return; 392 | } 393 | 394 | [self.installedView removeConstraint:self.layoutConstraint]; 395 | self.layoutConstraint = nil; 396 | self.installedView = nil; 397 | 398 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 399 | } 400 | 401 | @end 402 | --------------------------------------------------------------------------------