iOS常用代码

2018-08-29  本文已影响24人  rick灬

1.设置图片透明

//图片透明

- (UIImage *)imageWithColor:(UIColor *)color {

    CGRect rect = CGRectMake(0.0f,0.0f, 1.0f,1.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

用: friendbg.image = [self imageWithColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]];;

2.创建 nav 防止跳转失败

在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法里写

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    MainViewController *main = [[MainViewController alloc]init];

    UINavigationController*nav = [[UINavigationController alloc]initWithRootViewController:main];

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    //创建并初始化UITabBarController

    self.window.rootViewController = nav;

    nav.navigationBarHidden = YES;

    [self.window makeKeyAndVisible];

3.XIB TableviewCell 创建使用

cell.h 中+(instancetype)xibTableViewCell;

cell.m 中+(instancetype)xibTableViewCell {

    //在类方法中加载xib文件,注意:loadNibNamed:owner:options:这个方法返回的是NSArray,所以在后面加上firstObject或者lastObject或者[0]都可以;因为我们的Xib文件中,只有一个cell

    return [[[NSBundle mainBundle] loadNibNamed:@"treasureCell" owner:nil options:nil] lastObject];

}

控制器.m中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中

static NSString *cellIndentifier = @"treasureCell";//这里的cellID就是cell的xib对应的名称

    treasureCell *cell = (treasureCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    if(nil == cell) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIndentifier owner:self options:nil];

        cell = [nib objectAtIndex:0]; //中间宽线

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    return cell;

PCH 中使用

// 手机版本控制

#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGTH))

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)

#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)

#define IS_IPHONE_6p (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

#define ceshiURL @"http://192.168.2.84:5000"//利好利空测试服务器

//判断是不是IPhoneX

#define isPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

// NSUserDefaults

#define UserSet(obj, key) [[NSUserDefaults standardUserDefaults] setObject:obj forKey:key]

#define UserObj(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]

#define UserSave [[NSUserDefaults standardUserDefaults] synchronize]

// 登录状态

#define IsLogin [[[NSUserDefaults standardUserDefaults] objectForKey:@"UseIsLogin"] isEqualToString:@"1"] ? 1 : 0

//获取屏幕宽高

#define KScreenWidth [[UIScreen mainScreen]bounds].size.width

#define KScreenHeight [[UIScreen mainScreen]bounds].size.height

#define kScreen_Bounds [UIScreen mainScreen].bounds

#define kScreenBg [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];

//颜色

#define BGColor UICOLOR_HEX(0xfff8f8f8)//主背景颜色 (灰色)

#define MainColor UICOLOR_HEX(0xfffab615)//主颜色 (黄色)

#define TitleColor UICOLOR_HEX(0xff333333)//字颜色 (灰黑色)

#define MainWrightColor UICOLOR_HEX(0xffFFFFFF)//主白色颜色 (白色)

#define MainBlueColor UICOLOR_HEX(0xff08c9e6)//主蓝色颜色 (蓝色)

#define MainBlColor UICOLOR_HEX(0xff008fd1)//背景浅蓝色

#define MainTextColor UICOLOR_HEX(0xff00faeb)//文字淡蓝色

//获取16进制32位色的UIColor对象

#define UICOLOR_HEX(x) \

[UIColor \

colorWithRed:(float)((float)((x>>16)&0xFF)/0xFF) \

green:(float)((float)((x>>8)&0xFF)/0xFF) \

blue:(float)((float)(x&0xFF)/(float)0xFF) \

alpha:(float)((float)((x>>24)&0xFF)/(float)0xFF) \

]

改变状态栏文字颜色

////设置状态栏字体提

- (UIStatusBarStyle)preferredStatusBarStyle

{

    return UIStatusBarStyleLightContent;//为白色

     return UIStatusBarStyleDefault;//黑色

}

上一篇下一篇

猜你喜欢

热点阅读