iOS开发中简单实用的几个小技巧

2020-01-18 17:47:28王冬梅

前言

本文记录了在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);

ios开发小技巧,ios简单开发,ios小技巧

技巧2:设置导航栏透明,title与BarButtonItem不透明


[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

self.navigationController.navigationBar.translucent = YES;

ios开发小技巧,ios简单开发,ios小技巧

技巧3:设置导航栏无边框


self.navigationController.navigationBar.shadowImage = [UIImage new];

ios开发小技巧,ios简单开发,ios小技巧

技巧4: 随视图的滚动导航栏隐藏与显示(一句代码即可)


self.navigationController.hidesBarsOnSwipe = Yes;

ios开发小技巧,ios简单开发,ios小技巧

技巧5:简单好用的获取当前时间戳


 //时间戳
 time_t now;
 time(&now);
 NSLog(@"---%ld",now);

ios开发小技巧,ios简单开发,ios小技巧

技巧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;