iOS小方法小工具

當后臺傳值類型為long

前端需要用NSString類型接收

當需要與特定值進行比較時,需要把該值用doubleValue轉(zhuǎn)化后與數(shù)值比較,不能用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)

當視圖數(shù)據(jù)為空時,需顯示默認視圖。

可以在tableView創(chuàng)建的cell個數(shù)的時候判斷是否有內(nèi)容,如果內(nèi)容為空,顯示默認視圖,否則移除。

待:此方法不是最優(yōu)化方法,需想出更一勞永逸的解決方法。

向數(shù)組中插入多個元素

數(shù)組操作中不僅能插入一個元素,還能插入多個元素首位或者末尾

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];

    //設置裁剪區(qū)域
    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();

    //結(jié)束上下文
    UIGraphicsEndImageContext();
    //顯示新圖片
    //self.iconImageView.image = newImage;
    
    return newImage;
}

UIImageView旋轉(zhuǎn)

//需要注意,這里之所以用(0.000001 - M_PI),是因為transform動畫旋轉(zhuǎn)會選擇最近的路徑進行旋轉(zhuǎn),默認是順時針,如果直接選擇- M_PI,那么箭頭只會順時針旋轉(zhuǎn),不會逆時針旋轉(zhuǎn)
// 使用了(0.000001 - M_PI),那么它會選擇近的路徑旋轉(zhuǎn),就不會順時針旋轉(zhuǎn)了
//大部分APP的下拉刷新基本都是箭頭逆時針回旋,并不是一直順時針旋轉(zhuǎn)
CGFloat angle = 0.000001 - M_PI  ;
self.headerBtn.imageView.transform = CGAffineTransformMakeRotation(angle);

UIEdgeInsets和UIOffsets

UIOffsetview距離父視圖superView的邊距

UIEdgeInsets內(nèi)邊距view內(nèi)的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屬性,可以直接調(diào)用:

[[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表示的是內(nèi)容區(qū)域的大小

contentOffset 是scrollview當前顯示區(qū)域頂點相對于frame頂點的偏移量,可以理解為contentview的頂點相對于scrollerVIew的frame的偏移量。

contentInset表示contentView.frame.orgin與scrollerView.frame.orgin的關系??梢灶惐扔赾ss里的padding。

參考contentSize、contentOffset和contentInset的圖解辨別

UIButton之圖片、文字

參考UIButton 的 imageEdgeInsets 和 titleEdgeInsets

UIScrollerView偏移問題

//  public var automaticallyAdjustsScrollViewInsets: Bool // Defaults to YES
// 這個是scrollview自動調(diào)整.當scrollview是第一個view時,系統(tǒng)會自動調(diào)整,否則不會自動調(diào)整64
  view.automaticallyAdjustsScrollViewInsets = false
// 去掉自動調(diào)整后,我們可以手動將tableview的contentInset調(diào)整64,就能完成我們的需求
  tableView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)

當有tabBarController時,推出下一個界面時,隱藏tabbar*

hidesBottomBarWhenPushed = true

HTTP類封裝:判斷用戶權限、是否登陸操作

需要封裝一個HTTP相關的類,在收到后臺的初始數(shù)據(jù)時進行處理,然后再封裝后傳出去

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容