iOS 一些简单但是实用的知识点

1. 点击页面,收回键盘

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITextField *fd1 = (UITextField *)[self.view viewWithTag:100];

    [fd1 resignFirstResponder];

  //[self.view endEditing:YES];

}

 

2. label的自适应宽度

_label.adjustsFontSizeToFitWidth = YES;

 

3.文件单独支持手动内存

-fno-objc-arc

文件单独支持自动内存

-f-objc-arc

 

4.沙盒路径

NSHomeDirectory()

5.MJ刷新 Xcode6

1. MJRefreshConst.m 里面 会报错: unknown type ‘NSString‘...

原因:  xcode6 取消.pch文件, 所以没有导入 foundation和uikit框架

解决方法: 在MJRefreshConst.m头部加上

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

2.

objc_msgSend(self.beginRefreshingTaget, self.beginRefreshingAction, self);

Too many arguments to function call, expected 0, have 3

经过几番周折,终于叨叨解决方案了

选中项目 - Project - Build Settings - ENABLE_STRICT_OBJC_MSGSEND  将其设置为 NO 即可

6.Xcode6 自己添加PCH文件设置其路径

$(SRCROOT)/MC/Prefix.pch

hei

[UIColor colorWithRed:35.0/255 green:35.0/255 blue:35.0/255 alpha:1];

hui

 [UIColor colorWithRed:112.0/255 green:112.0/255 blue:112.0/255 alpha:1];

7.当前屏幕宽

[UIScreen mainScreen].bounds.size.width

8.PCH

#ifdef __OBJC__

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

#endif

 在Build Settings中改变Prefix Header的

$(SRCROOT)/Wei/Prefix.pch

 

9.网址中文重新编码

ASIHTTPRequest *_re = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]];

 

10.设置背景色

    float a = arc4random()%256/255.0;

    float b = arc4random()%256/255.0;

    float c = arc4random()%256/255.0;

    

    self.view.backgroundColor = [UIColor colorWithRed:a green:b blue:c alpha:1];

 

11.Cell

//设置cell的阴影线为透明

    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

 

//cell的选中的背景色

cell.selectionStyle = UITableViewCellSelectionStyleNone;

 

12.启动图

 

640*960

640*1136

 

58*58

87*87

80*80

120*120

120*120

180*180

 

13.从这张图片汲取颜色,作为背景色

self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"logo_bg_2.png"]];

 

14.十六进制配颜色

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

 UIColorFromRGB(0xf7f7f7);

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

 

NSString *color = [[NSString alloc] initWithString:model.drug_hot_color];

leibiao.backgroundColor = [AOColorFormat colorWithHexString:color];

  

15.最普通动画: 

//开始动画 

[UIView beginAnimations:nil context:nil];  

//设定动画持续时间 

[UIView setAnimationDuration:2]; 

//动画的内容 

frame.origin.x += 150; 

[img setFrame:frame]; 

//动画结束 

[UIView commitAnimations]; 

 

16.圆角与边框

//给图层添加背景图片:

//myView.layer.contents = (id)[UIImage imageNamed:@"view_BG.png"].CGImage;         

//将图层的边框设置为圆

myWebView.layer.cornerRadius = 8;

myWebView.layer.masksToBounds = YES;  

//给图层添加一个有色边框

myWebView.layer.borderWidth = 5;

myWebView.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1] CGColor];

 

17.文字高度与宽度

CGSize textSize = [showText sizeWithFont:[UIFont systemFontOfSize:12]];

CGRect rect = [model.drug_hot_name boundingRectWithSize:CGSizeMake(WIDTH, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font1+2] forKey:NSFontAttributeName] context:nil];

 

 

18.数组去重

    NSMutableArray *categoryArray = [[NSMutableArray alloc] init];

    for (unsigned i = 0; i < [nameArray count]; i++){

        if ([categoryArray containsObject:[nameArray objectAtIndex:i]] == NO){

            [categoryArray addObject:[nameArray objectAtIndex:i]];

        }

        

    }

 

19.iOS

[1]概念

iOS的版本号,一个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击“Summary”后看到。 Version在plist文件中的key是“CFBundleShortVersionString”,和AppStore上的版本号保持一致,Build在plist中的key是“CFBundleVersion”,代表build的版本号,该值每次build之后都应该增加1。这两个值都可以在程序中通过下面的代码获得:

[[[NSBundle mainBundle] infoDictionary] valueForKey:@"key"]

 

[2]具体实现

代码实现获得应用的Verison号:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"xxxxxx"]

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"xxxxxx"];

 

获得build号:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"xxxxxxxx"]

 

20.设置按钮不能同时被点击

[starBtn3 setExclusiveTouch:YES];

 

21.UIView基础【动画效果】

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:0.5];

//do something

[UIView commitAnimations];

 

[UIView animateWithDuration:.2 animations:^{

        huang.frame = CGRectMake(WIDTH+20, 0, WIDTH, HEIGHT);

    }];

[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(time) userInfo:nil repeats:NO];

 

22.设置rootViewController 

AppDelegate * dele = (AppDelegate *)app.delegate;

dele.window.rootViewController = nc;

 

23.UIImageView 的contentMode属性

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill

UIViewContentModeRedraw

UIViewContentModeCenter

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 

ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。

 

24.去掉btn点击事件,加新的点击事件

[pass_button removeTarget:self action:@selector(oneClick) forControlEvents:UIControlEventTouchUpInside];

[pass_button addTarget:self action:@selector(twoClick) forControlEvents:UIControlEventTouchUpInside];

 

25.求字符串宽高

+(CGRect)stringRectfor:(NSString*)string font:(UIFont*)font width:(CGFloat)width

{

 

    CGRect textgetRect;

    if( [[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)

    {

        NSDictionary *dict = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];

        textgetRect = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];

    }

    else

    {

        textgetRect.size = [string sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];

    }

    return textgetRect;

}

 

26.判断当前navigationController有几层

if ([self.navigationController.viewControllers count] == 1)

{

     bottomPadding = 49;

}else{

     bottomPadding = 0;

}

 

posted @ 2015-08-10 10:54  樱木布莱恩特  阅读(214)  评论(0)    收藏  举报