iOS开发过程中的一些小技巧,绝对有你想要的

1.UISearchBar的使用

 1.去除UISearchbar边框,换掉UISearchbar的背景图片即可

 UIImageView *imageView =[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"searchBarBGImage"]];

 self.searchBar.backgroundImage =imageView.image;

 2.修改搜索输入文本的背景

[_searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"login_btn_input_side.png"]forState:UIControlStateNormal];

 3.修改右侧取消按钮样式

 -(void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar

{

    [searchBarsetShowsCancelButton:YES animated:YES];

    UIView *searchBarView =searchBar.subviews[0];

    // 修改UISearchBar右侧的取消按钮文字颜色及背景图片

    for (UIView *searchbuttons in[searchBarView subviews]){

        if ([searchbuttonsisKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {

            UIButton *cancelButton =(UIButton*)searchbuttons;

            [cancelButtonsetTitle:@"取消" forState:UIControlStateNormal];

            // 修改文字颜色

            [cancelButtonsetTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

            [cancelButtonsetTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

        }

    }

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{

    [searchBarresignFirstResponder];

    [searchBarsetShowsCancelButton:NO animated:YES];

}

2.UITextField不显示光标

1.UITextField不显示光标

textField.tintColor=[UIColor clearColor];

2.修改UITextField中Placeholder的文字颜色

[textField setValue:[UIColor redColor]forKeyPath:@"_placeholderLabel.textColor"];

3.UITableView隐藏多余的cell

tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];

4.UITableView在plane模式下让section头部不停留,在scrollViewDidScroll方法里面写

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGFloat sectionHeaderHeight =25;//头部的高度

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

        }

    }

}

5.UITableView的Group样式下顶部空白处理

//分组列表头部空白处理

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.1)];

self.tableView.tableHeaderView = view;

6.两种方法删除NSUserDefaults所有记录

//方法一

NSString *appDomain = [[NSBundle mainBundle]bundleIdentifier];

[[NSUserDefaults standardUserDefaults]removePersistentDomainForName:appDomain];

//方法二

- (void)resetDefaults

{

NSUserDefaults * defs = [NSUserDefaultsstandardUserDefaults];

NSDictionary * dict = [defs dictionaryRepresentation];

for (id key in dict)

{

[defs removeObjectForKey:key];

}

[defs synchronize];

}

7.iOS 获取汉字的拼音

+ (NSString *)transform:(NSString *)chinese

{

//将NSString装换成NSMutableString

NSMutableString *pinyin = [chinese mutableCopy];

//将汉字转换为拼音(带音标)

CFStringTransform((__bridge CFMutableStringRef)pinyin,NULL, kCFStringTransformMandarinLatin, NO);

NSLog(@"%@", pinyin);

//去掉拼音的音标

CFStringTransform((__bridge CFMutableStringRef)pinyin,NULL, kCFStringTransformStripCombiningMarks, NO);

NSLog(@"%@", pinyin);

//返回最近结果

return pinyin;

}

8.手动更改iOS状态栏的颜色

- (void)setStatusBarBackgroundColor:(UIColor *)color

{

  UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"]valueForKey:@"statusBar"];

  if ([statusBarrespondsToSelector:@selector(setBackgroundColor:)])

    {

      statusBar.backgroundColor = color;

    }

}

9.判断当前ViewController是push还是present的方式显示的

NSArray *viewcontrollers=self.navigationController.viewControllers;

if (viewcontrollers.count > 1)

{

if ([viewcontrollers objectAtIndex:viewcontrollers.count- 1] == self)

{

//push方式

[self.navigationControllerpopViewControllerAnimated:YES];

}

}

else

{

//present方式

[self dismissViewControllerAnimated:YES completion:nil];

}

10.GCD timer倒计时定时器

dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0,queue);

dispatch_source_set_timer(timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC, 0); //每秒执行

dispatch_source_set_event_handler(timer, ^{

//@"倒计时结束,关闭"

dispatch_source_cancel(timer);

dispatch_async(dispatch_get_main_queue(), ^{

});

});

dispatch_resume(timer);

11.导航栏设置

1.改变导航栏标题颜色

self.navigationItem.rightBarButtonItem.tintColor =[UIColor whiteColor];

[self.navigationController.navigationBarsetTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColorwhiteColor]}];

2.改变导航栏背景颜色

 [self.navigationController.navigationBarsetBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

 [self.navigationController.navigationBarsetShadowImage:[UIImage new]];

[self.navigationController.navigationBarsetBarTintColor:[UIColor colorWithRed:128/255.0f green:128/255.0fblue:128/255.0f alpha:1]];

12.调整行间距

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:self.ruleLabel.text];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc] init];

[paragraphStylesetLineSpacing:10];

[attributedStringaddAttribute:NSParagraphStyleAttributeName value:paragraphStylerange:NSMakeRange(0, [self.ruleLabel.text length])];

13.用代码计算文字的宽高

-(CGSize)sizeWithText:(NSString *)text font:(UIFont*)font maxW:(CGFloat)maxW

{

   NSMutableDictionary *attrs=[NSMutableDictionary dictionary];

   attrs[NSFontAttributeName]=font;

    CGSizemaxSize=CGSizeMake(maxW, MAXFLOAT);

    return [textboundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOriginattributes:attrs context:nil].size;

}



//------------------华丽的分割线----------------//

未完待续... ...







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值