iOS小方法小工具

当后台传值类型为long

前端需要用NSString类型接收

当需要与特定值进行比较时,需要把该值用doubleValue转化后与数值比较,不能用NSString类型直接比较,因为后台可能传出的值为0.00型的或者0.0型的等,不能完全相等

Model *model = [Model alloc] init];
model.userID  = dict[@"id"];
if ([model.userID doubleValue] == 0)
  {
    //做需要的操作
  }

设置view的背景图片

view.layer.constonts = (__bridge id)image.CGImage;

界面通常为tableView或者collocationView布局。
(借鉴dzenbot/DZNEmptyDataSet)

当视图数据为空时,需显示默认视图。

可以在tableView创建的cell个数的时候判断是否有内容,如果内容为空,显示默认视图,否则移除。

待:此方法不是最优化方法,需想出更一劳永逸的解决方法。

向数组中插入多个元素

数组操作中不仅能插入一个元素,还能插入多个元素首位或者末尾

NSArray *addModel = @[kbOne,kbTwo];
NSRange range=NSMakeRange(0, addModel.count);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[self.modelsArray insertObjects:addModel atIndexes:indexSet];

裁剪圆形头像

#pragma mark - 裁剪圆形头像
- (UIImage *)imageWithClipImage:(UIImage *)image
                    borderWidth:(CGFloat)borderW
                     boderColor:(UIColor *)boderColor
{
    //加载image
    //UIImage *image = [UIImage imageNamed:imageName];
  
    //开启位图上下文
    CGSize roundSize = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 *borderW);
    UIGraphicsBeginImageContextWithOptions(roundSize, NO, 0.0);
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, roundSize.width,roundSize.height)];
    [boderColor set];
    [path fill];

    //设置裁剪区域
    CGRect clipRect = CGRectMake(borderW, borderW, image.size.width, image.size.height);
    path = [UIBezierPath bezierPathWithOvalInRect:clipRect];
    [path addClip];

    //绘制图片
    [image drawAtPoint:CGPointMake(borderW, borderW)];

    //从上下文中取出新的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    //结束上下文
    UIGraphicsEndImageContext();
    //显示新图片
    //self.iconImageView.image = newImage;
    
    return newImage;
}

UIImageView旋转

//需要注意,这里之所以用(0.000001 - M_PI),是因为transform动画旋转会选择最近的路径进行旋转,默认是顺时针,如果直接选择- M_PI,那么箭头只会顺时针旋转,不会逆时针旋转
// 使用了(0.000001 - M_PI),那么它会选择近的路径旋转,就不会顺时针旋转了
//大部分APP的下拉刷新基本都是箭头逆时针回旋,并不是一直顺时针旋转
CGFloat angle = 0.000001 - M_PI  ;
self.headerBtn.imageView.transform = CGAffineTransformMakeRotation(angle);

UIEdgeInsets和UIOffsets

UIOffsetview距离父视图superView的边距

UIEdgeInsets内边距view内的content距离view的边距

UIEdgeInsets是什么?

typedef struct UIEdgeInsets {
    CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;

三个UIEdgeInsets属性

@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets imageEdgeInsets; // default is UIEdgeInsetsZero

提示:UI_APPEARANCE_SELECTOR标记的属性都支持通过外观代理来定制。

举例,设置UIButton的contentEdgeInsets属性,可以直接调用:

[[UIButton appearance] setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
或者
  button.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0);

tableview的contentOffset和contentInset

@property(nonatomic)CGPoint contentOffset;    // default CGPointZero
@property(nonatomic)CGSize  contentSize;      // default CGSizeZero
@property(nonatomic)UIEdgeInsets contentInset;// default UIEdgeInsetsZero. add additional scroll area around content

contentSize表示的是内容区域的大小

contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,可以理解为contentview的顶点相对于scrollerVIew的frame的偏移量。

contentInset表示contentView.frame.orgin与scrollerView.frame.orgin的关系。可以类比于css里的padding。

参考contentSize、contentOffset和contentInset的图解辨别

UIButton之图片、文字

参考UIButton 的 imageEdgeInsets 和 titleEdgeInsets

UIScrollerView偏移问题

//  public var automaticallyAdjustsScrollViewInsets: Bool // Defaults to YES
// 这个是scrollview自动调整.当scrollview是第一个view时,系统会自动调整,否则不会自动调整64
  view.automaticallyAdjustsScrollViewInsets = false
// 去掉自动调整后,我们可以手动将tableview的contentInset调整64,就能完成我们的需求
  tableView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)

当有tabBarController时,推出下一个界面时,隐藏tabbar*

hidesBottomBarWhenPushed = true

HTTP类封装:判断用户权限、是否登陆操作

需要封装一个HTTP相关的类,在收到后台的初始数据时进行处理,然后再封装后传出去

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 解决添加到ScrollView上的UITableView控件自动向下偏移64像素的问题 首先理解1:即使UITab...
    CoderZb阅读 10,736评论 1 8
  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 8,858评论 0 15
  • 日期转换为时间戳 (日期转换为秒数) NSDate *date = [NSDate date]; NSLog(@"...
    BiniOSdeveloper阅读 2,555评论 0 2
  • 安排值班表再接到吩咐要拔掉电源时,差点有大部队撤离后方回家过年时的错觉。周围一片寂静,整幢楼空荡的空间里发出风吹过...
    你在微笑吗阅读 2,849评论 0 0
  • 出个选择题供人们选择,会让人觉得做决定的主动劝在自己手里,更容易去做选择,而一味的劝说,让人生出抵触情绪,反而会拒绝。
    栀子悠声阅读 1,112评论 0 1

友情链接更多精彩内容