Skip to content

Instantly share code, notes, and snippets.

@mrdaios
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdaios/30b03a388623b38f2819 to your computer and use it in GitHub Desktop.
Save mrdaios/30b03a388623b38f2819 to your computer and use it in GitHub Desktop.
UIViewController常用配置。
//
// HaidoraViewControllerConfig.h
// TestTemplate
//
// Created by Dailingchi on 15/6/1.
// Copyright (c) 2015年 Haidora. All rights reserved.
//
#ifndef Haidora_HaidoraViewControllerConfig_h
#define Haidora_HaidoraViewControllerConfig_h
#if __has_include("UIViewController+HDLayout.h")
#import "UIViewController+HDLayout.h"
#endif
#if __has_include("UIViewController+HDStyle.h")
#import "UIViewController+HDStyle.h"
#endif
#endif
//
// UIViewController+HDLayout.h
// TestTemplate
//
// Created by Dailingchi on 15/6/1.
// Copyright (c) 2015年 Haidora. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
* UIViewController,布局相关配置
*/
@interface UIViewController (HDLayout)
/**
* 布局方式
*/
- (void)configEdgesForExtendedLayout;
@end
/**
* 布局相关配置
*/
@interface HDLayoutConfig : NSObject
/**
* 自动配置UIViewController默认信息,默认为NO.
*/
@property (nonatomic, assign) BOOL autoConfig;
/**
* ViewController前缀(为了区分自定义的VC和第三方VC),默认为空.
*/
@property (nonatomic, strong) NSString *viewControllerPrefix;
+ (HDLayoutConfig *)sharedInstance;
@end
//
// UIViewController+HDLayout.m
// TestTemplate
//
// Created by Dailingchi on 15/6/1.
// Copyright (c) 2015年 Haidora. All rights reserved.
//
#import "UIViewController+HDLayout.h"
#import <Aspects.h>
@implementation UIViewController (HDLayout)
+ (void)load
{
NSError *error;
[UIViewController
aspect_hookSelector:@selector(viewDidLoad)
withOptions:AspectPositionBefore
usingBlock:^(id<AspectInfo> aspectInfo) {
//注入逻辑
UIViewController *viewController = [aspectInfo instance];
if ([HDLayoutConfig sharedInstance].autoConfig &&
[HDLayoutConfig sharedInstance].viewControllerPrefix &&
[NSStringFromClass([viewController class])
hasPrefix:[HDLayoutConfig sharedInstance].viewControllerPrefix])
{
[viewController configEdgesForExtendedLayout];
}
} error:&error];
#ifdef DEBUG
if (error)
{
NSLog(@"%@", error);
}
#endif
}
- (void)configEdgesForExtendedLayout
{
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
{
[self performSelector:@selector(setEdgesForExtendedLayout:) withObject:UIRectEdgeNone];
}
}
@end
@implementation HDLayoutConfig
static HDLayoutConfig *sharedHDLayoutConfig = nil;
+ (HDLayoutConfig *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedHDLayoutConfig = [[HDLayoutConfig alloc] init];
sharedHDLayoutConfig.autoConfig = NO;
});
return sharedHDLayoutConfig;
}
@end
//
// UIViewController+HDStyle.h
// TestTemplate
//
// Created by Dailingchi on 15/6/1.
// Copyright (c) 2015年 Haidora. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
* UIViewController UI相关的配置
*/
@interface UIViewController (HDStyle)
/**
* 设置View(TableView背景),默认加载HDStyleConfig中的backGroundColor.
*/
- (void)configDefaultBackgroundWith:(UIColor *)backGroundColor;
@end
/**
* UI相关的配置
*/
@interface HDStyleConfig : NSObject
/**
* 自动配置UIViewController默认信息,默认为NO.
*/
@property (nonatomic, assign) BOOL autoConfig;
/**
* ViewController前缀(为了区分自定义的VC和第三方VC),默认为空.
*/
@property (nonatomic, strong) NSString *viewControllerPrefix;
/**
* 背景颜色,默认为[UIColor whiteColor].
*/
@property (nonatomic, strong) UIColor *backGroundColor;
+ (HDStyleConfig *)sharedInstance;
@end
//
// UIViewController+HDStyle.m
// TestTemplate
//
// Created by Dailingchi on 15/6/1.
// Copyright (c) 2015年 Haidora. All rights reserved.
//
#import "UIViewController+HDStyle.h"
#import <Aspects.h>
@implementation UIViewController (HDStyle)
+ (void)load
{
NSError *error;
[UIViewController
aspect_hookSelector:@selector(viewDidLoad)
withOptions:AspectPositionBefore
usingBlock:^(id<AspectInfo> aspectInfo) {
//注入逻辑
UIViewController *viewController = [aspectInfo instance];
if ([HDStyleConfig sharedInstance].autoConfig &&
[HDStyleConfig sharedInstance].viewControllerPrefix &&
[NSStringFromClass([viewController class])
hasPrefix:[HDStyleConfig sharedInstance].viewControllerPrefix])
{
[viewController configDefaultBackgroundWith:[HDStyleConfig sharedInstance]
.backGroundColor];
}
} error:&error];
#ifdef DEBUG
if (error)
{
NSLog(@"%@", error);
}
#endif
}
- (void)configDefaultBackgroundWith:(UIColor *)backGroundColor
{
if ([self isKindOfClass:[UIViewController class]])
{
self.view.backgroundColor = backGroundColor;
}
else if ([self isKindOfClass:[UITableViewController class]])
{
((UITableViewController *)self).tableView.backgroundColor = backGroundColor;
}
}
@end
@implementation HDStyleConfig
static HDStyleConfig *sharedHDStyleConfig = nil;
+ (HDStyleConfig *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedHDStyleConfig = [[HDStyleConfig alloc] init];
sharedHDStyleConfig.autoConfig = NO;
sharedHDStyleConfig.backGroundColor = [UIColor whiteColor];
});
return sharedHDStyleConfig;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment