push到下一个界面需要隐藏tabbar
viewCtr.hidesBottomBarWhenPushed = YES;
隐藏leftBarButtonItem 或者 rightBarButtonItem,隐藏返回按钮
//自定义
self.navigationItem.leftBarButtonItem = nil;
//系统
self.navigationItem.hidesBackButton = YES;
跳转到下一界面会有停顿的阴影,在下一界面viewDidLoad中写入以下代码
[self.navigationController setNavigationBarHidden:NO animated:YES]];
self.view.backgroundColor = [UIColor whiteColor];
根据内容UILabel自适应高度
NSString * string = @"2016-11-15";
CGRect sizeLabel = [string boundingRectWithSize:CGSizeMake(200, 100000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 150,, sizeLabel.size.width, sizeLabel.size.height)];
introduceLabel.text = string;
introduceLabel.font = [UIFont systemFontOfSize:13];
introduceLabel.numberOfLines = 0;
获取webview的高度
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
webview中的图片自适应屏幕
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *js = @"function imgAutoFit() { \
var imgs = document.getElementsByTagName('img'); \
for (var i = 0; i < imgs.length; ++i) { \
var img = imgs[i]; \
img.style.maxWidth = %f; \
} \
}";
js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
[webView stringByEvaluatingJavaScriptFromString:js];
[webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
}
设置UIWebView字体大小
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[_myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '70%'"];
}
设置WKWebView字体大小
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '250%'";
[webView evaluateJavaScript:str completionHandler:nil];
}
ILabel设置行间距等属性:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:5];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, contentLabel.text.length)];
contentLabel.attributedText = attributedString;
//调节高度
CGSize size = CGSizeMake(width, 500000);
CGSize labelSize = [contentLabel sizeThatFits:size];
根据文件名来获取文件路径(Document目录下)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:@"文件名字”];
resource目录下
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@“文件名字"];
Cache目录下
NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cache objectAtIndex:0];
NSString *voiceName = [cachePath stringByAppendingPathComponent:[sender stringByAppendingString:@".wav"]];
UITableView消除多余的cell分割线
tableView.tableFooterView = [[UIView alloc] init];
修改UITextfield中Placeholder的文字颜色和大小
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
去除导航栏返回的back标题
[[UIBarButtonItemappearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];
禁止锁屏
[UIApplication sharedApplication].idleTimerDisabled = YES;
或
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
iOS跳转到AppStore下载应用评分
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APPID"]];
判断当前ViewController是push还是present的方式显示的
NSArray *viewcontrollers=self.navigationController.viewControllers;
if (viewcontrollers.count > 1){
if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self){
//push方式
[self.navigationController popViewControllerAnimated:YES];
}
}
else{
//present方式
[self dismissViewControllerAnimated:YES completion:nil];
}
Cocoapods pod install/update更新慢的问题
pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
加后面的参数会略过升级Cocoapods的spec仓库,速度回提升很多
在APP中打开系统设置界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
AFN移除JSON中的NSNull
AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
response.removesKeysWithNullValues = YES;
获取cell在某视图的位置
CGRect cellRect = [collectionView convertRect:cell.frame toView:_albumCollectionView];
CGRect cellFrame = [collectionView convertRect:cellRect toView:self.view];
获取Window有以下几种方式
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UIWindow *window = [[UIApplication sharedApplication].delegate window]
UIWindow *window = [[UIApplication sharedApplication].keyWindow ];
UIWindow *window = [UIApplication sharedApplication].windows[0];
获取cell所在的tableView
- (UITableView *)myTableView
{
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UITableView class]]) {
return (UITableView *)nextResponder;
}
}
return nil;
}
获取cell所在的viewController
- (UIViewController *)myViewCtr
{
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
保存图片到本地
UIImageWriteToSavedPhotosAlbum(self.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
// 实现回调
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(id)contextInfo
{
if (!error) {
NSLog(@"保存成功");
}else{
NSLog(@"保存失败");
}
}
压缩图片质量
-(UIImage *)reduceImage:(UIImage *)image percent:(float)percent
{
NSData *imageData = UIImageJPEGRepresentation(image, percent);
UIImage *newImage = [UIImage imageWithData:imageData];
return newImage;
}
压缩图片大小
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}