判断iPhoneX++
1 | static inline BOOL isIPhoneXSeries() { |
Button 大小
1 | - (CGSize)intrinsicContentSize { |
截屏
1 | - (UIImage *)screenshot { |
设置pageControl单页不显示
1 | self.pageControl.hidesForSinglePage = YES; |
让数组中的所有对象都执行一下方法
1 | [self.view.subViews makeObjectsPerformSelector:@selecter(removeFromSuperView)]; |
scrollsToTop
scrollsToTop是UIScrollView的一个属性,主要用于点击设备的状态栏时,是scrollsToTop == YES的控件滚动返回至顶部。每一个默认的UIScrollView的实例,他的scrollsToTop属性默认为YES,所以要实现某一UIScrollView的实例点击设备状态栏返回顶部,则需要关闭其他的UIScrollView的实例的scrollsToTop属性为NO。
SB跳转
1 | // 跳转 |
iOS 10拨打系统电话发现弹出框会延迟2s左右
解决方案一:1
2
3
4
5
6
7
8
9
10
11
12
13+ (void)callPhone:(NSString *)phoneNum {
if (phoneNum.length == 0) {
return;
}
NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", phoneNum];
NSComparisonResult compare = [[UIDevice currentDevice].systemVersion compare:@"10.0"];
if (compare == NSOrderedDescending || compare == NSOrderedSame) {
/// 大于等于10.0系统使用此openURL方法
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
}
}
解决方案二:1
2
3
4
5
6
7
8
9+ (void)callPhone:(NSString *)phoneNum {
if (phoneNum.length == 0) {
return;
}
NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", phoneNum];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
});
}
修改TableViewHeaderView字体大小
1 | - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { |
heightForHeaderInSection设置高度无效
1 | iOS11默认开启Self-Sizing,关闭Self-Sizing即可。 |
屏蔽一些无知的警告
Xcode8里边 Edit Scheme-> Run -> Arguments, 在Environment Variables里边添加 OS_ACTIVITY_MODE = Disable · 如果写了之后还是打印log,请重新勾选对勾,就可以解决了。
UITableView收起键盘
1 | tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; |
长按复制功能
1 | - (void)viewDidLoad { |
禁止手机睡眠
1 | [UIApplication sharedApplication].idleTimerDisabled = YES; |
禁用button高亮
1 | button.adjustsImageWhenHighlighted = NO; |
控制屏幕旋转,在控制器中写
1 | /** 是否支持自动转屏 */ |
获取app缓存大小
1 | - (CGFloat)getCachSize { |
UITextView中打开或禁用复制,剪切,选择,全选等功能
1 | // 继承UITextView重写这个方法 |
禁止textField和textView的复制粘贴菜单
1 | - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ |
为一个view添加虚线边框
1 | CAShapeLayer *border = [CAShapeLayer layer]; |
设置tableView分割线颜色
1 | [self.tableView setSeparatorColor:[UIColor myColor]]; |
UIImage和base64互转
1 | - (NSString *)encodeToBase64String:(UIImage *)image { |
摇一摇功能
1 | 1、打开摇一摇功能 |
发起QQ聊天
QQ开通推广服务以接收陌生消息1
2
3
4
5
6
7
8//是否安装QQ
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
//用来接收临时消息的客服QQ号码(注意此QQ号需开通QQ推广功能,否则陌生人向他发送消息会失败)
NSString *QQ = @"1099779883";
//调用QQ客户端,发起QQ临时会话
NSString *url = [NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",QQ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
MJRefresh
webview去掉长按后的选择和复制菜单
1 | 1. webview 执行 js |
XIB 可见属性设置(eg: 圆角)
OC
:IB_DESIGNABLE
和IBInspectable
1
2
3
4
5
6
7
8
9
10
11
12
13// .h
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
// .m
IB_DESIGNABLE @implementation KLButton
- (void)setCornerRadius:(CGFloat)cornerRadius {
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = (cornerRadius > 0);
}
- (CGFloat)cornerRadius {
return self.layer.cornerRadius;
}Swift
:IBInspectable
和IBDesignable
方法不确定的参数 va_arg 宏的使用
当我们方法需要传入多个不确定的参数时(如:[NSArray arrayWithObjects]), 我们可以使用 ,… 的形式表示,当要取传入的参数的值时就需要用到 va_arg 宏去取。
例如:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17- (void)viewDidLoad {
[super viewDidLoad];
[self testMethod:@"123",@"456",@"789",nil];
}
- (void)testMethod:(NSString *)test,.. NS_REQUIRES_NIL_TERMINATION {
va_list argList;
va_start(argList, test);
{
for (id params = test; params != nil; params = va_arg(argList, id)){
NSLog(@"id = %@---class = %@",params,NSStringFromClass([params class]));
}
}
va_end(argList);
}
去除数组中重复的对象
1 | NSArray *newArr = [oldArr valueForKeyPath:@"@distinctUnionOfObjects.self"]; |
删除NSUserDefaults所有记录
1 | //方法一 |
UITableViewCell 分割线置顶
1 | if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { |
设置UIViewController的view的top在navigationBar下面 & bottom位置在Tabbar的上面
1 | tabBarController.tabBar.translucent = NO; |
禁用button高亮,点击变灰的效果
1 | button.adjustsImageWhenHighlighted = NO; |
自定义cell选中背景颜色
1 | UIView *bgColorView = [[UIView alloc] init]; |
UILabel设置内边距
1 | 子类化UILabel,重写drawTextInRect方法 |
UILabel设置文字描边
1 | 子类化UILabel,重写drawTextInRect方法 |
改变UISlider的进度条高度
需要继承UISlider并t重载trackRectForBounds方法1
2
3- (CGRect)trackRectForBounds:(CGRect)bounds {
return CGRectMake(0, 0, CGRectGetWidth(self.frame), 5.0);
}
iOS 11中隐藏section头尾的实现
1 | - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { |
更改UITabBar && UINavigationBar背景颜色
1 | [[UITabBar appearance] setBarTintColor:[UIColor redColor]]; |
更改UITabBar && UINavigationBar字体颜色
1 | //改变按钮navigationBar tintColor颜色 |