关闭

iOS开发中的一些小技巧

标签: iOS开发小技巧奇巧淫技注意事项
480人阅读 评论(0) 收藏 举报
分类:

废话不多说,直接上菜:


0、一行代码移除所有子视图【(面试中)笔试时遇到过多次,一行代码解决逼格高)】

[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

1、tableView去掉尾部多余的行/分割线

self.tableView.tableFooterView=[[UIView alloc] init]; //最后一行有分割线
self.tableView.tableFooterView=[[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 1.0, 1.0)]; //最后一行也没有分割线

2、tableView取消section的headerview在顶部悬停(粘性)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.myTableView)  
    {
        CGFloat sectionHeaderHeight = 46;  //Header高度 
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

如果也要取消section的footerview在底部的悬停(粘性),将tableView的Style改为Grouped即可。

3、AFNetworking中文字符转码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

4、任意地方查找ViewController

+ (id)viewControllerInStoryboard:(NSString *)storyboardName withIdentifier:(NSString*)identifier
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    return [storyboard instantiateViewControllerWithIdentifier:identifier];
}

5、万能收键盘

[self.view endEditing:YES];

6、iOS7+ 中 BarButtonItem 调整位置(常用于返回按钮左移以和iOS6保持一致)

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 0, 44, 44);
[aButton setBackgroundImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:aButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = @[negativeSpacer, aBarButtonItem];

7、颜色

#define ColorFromHex(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]
#define ColorFromR_G_B(r,g,b)      [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0]
#define ColorFromR_G_B_A(r,g,b,a)  [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
8、屏幕
#define Screen_Width    ([UIScreen mainScreen].bounds.size.width)     
#define Screen_Height   ([UIScreen mainScreen].bounds.size.height) 

9、蓝牙连接外设

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"did Discover Peripheral  ----> %@ ",peripheral);
    
    [self.peripherals addObject:peripheral]; //注意:一定要先保持对这个对象的引用(这里是加入一个数组),否则会因为没有引用计数而被回收,导致后续无法连接上
    [centralManager connectPeripheral:peripheral options:nil]; //连接一个蓝牙设备
}




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20245次
    • 积分:357
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:5篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论