您的位置:首页 > 移动开发 > IOS开发

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

2016-12-27 17:56 267 查看

1.UISearchBar的使用

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

 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 = [chines
aa1b
e 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;
}

//------------------华丽的分割线----------------//
未完待续... ...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: