iOS开发中简单实用的几个小技巧
时间:2020-10-26 22:59:18|栏目:iOS代码|点击: 次
前言
本文记录了在iOS开发过程中所遇到的小知识点,以及一些技巧,下面话不多说,来看看详细的介绍。
技巧1:UIButton图片与文字默认是左右排列,如何实现右左排列?
解决技巧:
button.transform = CGAffineTransformMakeScale(-1.0, 1.0); button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0); button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
技巧2:设置导航栏透明,title与BarButtonItem不透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.translucent = YES;
技巧3:设置导航栏无边框
self.navigationController.navigationBar.shadowImage = [UIImage new];
技巧4: 随视图的滚动导航栏隐藏与显示(一句代码即可)
self.navigationController.hidesBarsOnSwipe = Yes;
技巧5:简单好用的获取当前时间戳
//时间戳 time_t now; time(&now); NSLog(@"---%ld",now);
技巧6:只设置UIView的左上角和右上角的圆角 (四个圆角位置都可以选择)
UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview: blueView]; /*设置圆角位置的枚举参数 UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL */ UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:blueView.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(20.0, 20.0)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = blueView.bounds; maskLayer.path = maskPath.CGPath; blueView.layer.mask = maskLayer;
技巧7: 加载UIWebView后禁止用户复制剪切
// 控制器实现此方法 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:) || action == @selector(paste:)|| action == @selector(cut:)) { return NO; } return [super canPerformAction:action withSender:sender]; }
技巧8:跳转控制器隐藏tabbar一个一劳永逸的方法
// 创建一个Nav基类 重写pushViewController:方法 如下: -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { viewController.hidesBottomBarWhenPushed = YES; [super pushViewController:viewController animated:animated]; }
总结