在iOS开发中常常须要使用的或不经常使用的知识点的总结,几年的收藏和积累(踩过的坑)。ios
手机型号 | 屏幕尺寸 |
---|---|
iPhone 4 4s | 320 * 480 |
iPhone 5 5s | 320 * 568 |
iPhone 6 6s | 375 * 667 |
iphone 6 plus 6s plus | 414 * 736 |
1
2
3
|
UIColor *whiteColor = [UIColor whiteColor];
NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dic];
|
1
2
3
4
5
6
|
CGPoint itemSprite1position = CGPointMake(100, 200);
NSMutableArray * array = [[NSMutableArray alloc] initWithObjects:NSStringFromCGPoint(itemSprite1position),nil];
// 从数组中取值的过程是这样的:
CGPoint point = CGPointFromString([array objectAtIndex:0]);
NSLog(@"point is %@.", NSStringFromCGPoint(point));
|
谢谢@bigParis的建议,能够用NSValue进行基础数据的保存,用这个方法更加清晰明确。git
1
2
3
4
5
6
7
8
|
CGPoint itemSprite1position = CGPointMake(100, 200);
NSValue *originValue = [NSValue valueWithCGPoint:itemSprite1position];
NSMutableArray * array = [[NSMutableArray alloc] initWithObjects:originValue, nil];
// 从数组中取值的过程是这样的:
NSValue *currentValue = [array objectAtIndex:0];
CGPoint point = [currentValue CGPointValue];
NSLog(@"point is %@.", NSStringFromCGPoint(point));
|
如今Xcode7后OC支持泛型了,能够用NSMutableArray *array
来保存。github
1
2
3
4
5
6
|
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
NSLog(@"Alpha: %f", components[3]);
|
1
2
3
|
self.textField.placeholder = @"username is in here!";
[self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
|
1
|
static __inline__ CGFloat CGPointDistanceBetweenTwoPoints(CGPoint point1, CGPoint point2) { CGFloat dx = point2.x - point1.x; CGFloat dy = point2.y - point1.y; return sqrt(dx*dx + dy*dy);}
|
一、点击Return按扭时收起键盘web
1
2
3
4
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
|
二、点击背景View收起键盘(你的View必须是继承于UIControl)sql
1
|
[self.view endEditing:YES];
|
三、你能够在任何地方加上这句话,能够用来统一收起键盘json
1
|
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
|
将图片直接拖入image到ImagesQA.xcassets中时,图片的名字会保留。
这个时候若是图片的名字过长,那么这个名字会存入到ImagesQA.xcassets中,名字过长会引发SourceTree判断异常。windows
开始选择的,须要在继承UiPickerView,建立一个子类,在子类中重载数组
1
|
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
|
当[super hitTest:point withEvent:event]
返回不是nil的时候,说明是点击中UIPickerView中了。
结束选择的, 实现UIPickerView的delegate方法xcode
1
|
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
|
当调用这个方法的时候,说明选择已经结束了。缓存
当iOS模拟器 选择了Keybaord->Connect Hardware keyboard 后,不弹出键盘。
当代码中添加了
1
2
3
4
|
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
|
进行键盘事件的获取。那么在此情景下将不会调用- (void)keyboardWillHide
.
由于没有键盘的隐藏和显示。
使用了size classes后,在ios7的模拟器上出现了上面和下面部分的黑色
能够在General->App Icons and Launch Images->Launch Images Source中设置Images.xcassets来解决。
Font中设置不一样的size classes。
1
2
|
[self.label1 performSelectorOnMainThread:@selector(setText:) withObject:textDisplay
waitUntilDone:YES];
|
label1 为UILabel,当在子线程中,须要进行text的更新的时候,可使用这个方法来更新。
其余的UIView 也都是同样的。
像Messages app同样在滚动的时候可让键盘消失是一种很是好的体验。然而,将这种行为整合到你的app很难。幸运的是,苹果给UIScrollView添加了一个很好用的属性keyboardDismissMode,这样能够方便不少。
如今仅仅只须要在Storyboard中改变一个简单的属性,或者增长一行代码,你的app能够和办到和Messages app同样的事情了。
这个属性使用了新的UIScrollViewKeyboardDismissMode enum枚举类型。这个enum枚举类型可能的值以下:
1
2
3
4
5
|
typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
UIScrollViewKeyboardDismissModeNone,
UIScrollViewKeyboardDismissModeOnDrag, // dismisses the keyboard when a drag begins
UIScrollViewKeyboardDismissModeInteractive, // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
} NS_ENUM_AVAILABLE_IOS(7_0);
|
如下是让键盘能够在滚动的时候消失须要设置的属性:
将 sqlite3.dylib加载到framework
iOS7上,默认status bar字体颜色是黑色的,要修改成白色的须要在infoPlist里设置UIViewControllerBasedStatusBarAppearance为NO,而后在代码里添加:[application setStatusBarStyle:UIStatusBarStyleLightContent];
1
2
3
4
5
|
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *fattributes = [fm attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
NSLog(@"容量%lldG",[[fattributes objectForKey:NSFileSystemSize] longLongValue]/1000000000);
NSLog(@"可用%lldG",[[fattributes objectForKey:NSFileSystemFreeSize] longLongValue]/1000000000);
|
UIView设置了alpha值,但其中的内容也跟着变透明。有没有解决办法?
设置background color的颜色中的透明度
好比:
1
|
[self.testView setBackgroundColor:[UIColor colorWithRed:0.0 green:1.0 blue:1.0 alpha:0.5]];
|
设置了color的alpha, 就能够实现背景色有透明度,当其余sub views不受影响给color 添加 alpha,或修改alpha的值。
1
2
3
4
|
// Returns a color in the same color space as the receiver with the specified alpha component.
- (UIColor *)colorWithAlphaComponent:(CGFloat)alpha;
// eg.
[view.backgroundColor colorWithAlphaComponent:0.5];
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//将color转为UIImage
- (UIImage *)createImageWithColor:(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 *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
|
1
2
3
|
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
|
在NSRunLoop 中添加定时器.
Bundle identifier 是应用的标示符,代表应用和其余APP的区别。
eg. 获取到40年前的日期
1
2
3
4
|
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:-40];
self.birthDate = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
|
只需须要在info.plist中加入Status bar is initially hidden 设置为YES就好
Xcode 项目中咱们可使用 ARC 和非 ARC 的混合模式。
若是你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签。
若是你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件加入 -fno-objc-arc 标签。
添加标签的方法:
以前使用了NSString类的sizeWithFont:constrainedToSize:lineBreakMode:方法,可是该方法已经被iOS7 Deprecated了,而iOS7新出了一个boudingRectWithSize:options:attributes:context方法来代替。
而具体怎么使用呢,尤为那个attribute
1
2
|
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
CGSize size = [@"相关NSString" boundingRectWithSize:CGSizeMake(100, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
|
NSDate 在保存数据,传输数据中,通常最好使用UTC时间。
在显示到界面给用户看的时候,须要转换为本地时间。
若是在一个UIViewController A中有一个property属性为UIViewController B,实例化后,将BVC.view 添加到主UIViewController A.view上,若是在viewB上进行 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0);
的操做将会出现,“ Presenting view controllers on detached view controllers is discouraged ” 的问题。
觉得BVC已经present到AVC中了,因此再一次进行会出现错误。
可使用
1
2
3
4
5
|
[self.view.window.rootViewController presentViewController:imagePicker
animated:YES
completion:^{
NSLog(@"Finished");
}];
|
来解决。
UITableViewCell 属性 NSInteger indentationLevel 的使用, 对cell设置 indentationLevel的值,能够将cell 分级别。
还有 CGFloat indentationWidth; 属性,设置缩进的宽度。
总缩进的宽度: indentationLevel * indentationWidth
使用AirDrop 进行分享:
1
2
3
4
5
6
7
8
|
NSArray *array = @[@"test1", @"test2"];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil];
[self presentViewController:activityVC animated:YES
completion:^{
NSLog(@"Air");
}];
|
就能够弹出界面:
获取CGRect的height, 除了 self.createNewMessageTableView.frame.size.height
这样进行点语法获取。
还可使用CGRectGetHeight(self.createNewMessageTableView.frame)
进行直接获取。
除了这个方法还有 func CGRectGetWidth(rect: CGRect) -> CGFloat
等等简单地方法
1
2
3
4
|
func CGRectGetMinX(rect: CGRect) -> CGFloat
func CGRectGetMidX(rect: CGRect) -> CGFloat
func CGRectGetMaxX(rect: CGRect) -> CGFloat
func CGRectGetMinY(rect: CGRect) -> CGFloat
|
1
|
NSString *printPercentStr = [NSString stringWithFormat:@"%%"];
|
allentekiMac-mini:JiKaTongGit lihuaxie$ grep -r advertisingIdentifier .
grep: ./ios/Framework/AMapSearchKit.framework/Resources: No such file or directory
Binary file ./ios/Framework/MAMapKit.framework/MAMapKit matches
Binary file ./ios/Framework/MAMapKit.framework/Versions/2.4.1.e00ba6a/MAMapKit matches
Binary file ./ios/Framework/MAMapKit.framework/Versions/Current/MAMapKit matches
Binary file ./ios/JiKaTong.xcodeproj/project.xcworkspace/xcuserdata/lihuaxie.xcuserdatad/UserInterfaceState.xcuserstate matches
allentekiMac-mini:JiKaTongGit lihuaxie$
打开终端,到工程目录中, 输入:
grep -r advertisingIdentifier .
能够看到那些文件中用到了IDFA,若是用到了就会被显示出来。
1
2
|
// Disable user interaction when download finishes
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
|
status bar的颜色设置:
1
|
self.view.backgroundColor = COLOR_APP_MAIN;
|
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];
[view setBackgroundColor:COLOR_APP_MAIN];
[viewController.navigationController.navigationBar addSubview:view];
1
|
####三十5、NSDictionary 转 NSString
|
// Start
NSDictionary *parametersDic = [NSDictionary dictionaryWithObjectsAndKeys:
self.providerStr, KEY_LOGIN_PROVIDER,
token, KEY_TOKEN,
response, KEY_RESPONSE,
nil];
NSData jsonData = parametersDic == nil ? nil : [NSJSONSerialization dataWithJSONObject:parametersDic options:0 error:nil];
NSString requestBody = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
1
2
3
4
5
6
7
8
9
10
|
将
dictionary 转化为 NSData, data 转化为 string .
####三十6、iOS7 中UIButton setImage 没有起做用
若是在
iOS7 中进行设置image 没有生效。
那么说明
UIButton的 enable 属性没有生效是NO的。 **须要设置enable 为YES。**
####三十7、User-Agent 判断设备
UIWebView 会根据User-Agent 的值来判断须要显示哪一个界面。
若是须要设置为全局,那么直接在应用启动的时候加载。
|
(void)appendUserAgent
{
NSString oldAgent = [self.WebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString newAgent = [oldAgent stringByAppendingString:@"iOS"];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
1
|
newAgent, @"UserAgent", nil];
|
[[NSUserDefaults standardUserDefaults] registerDefaults:dic];
}
@“iOS” 为添加的自定义。
当UIpasteboard的string 设置为@“” 时,那么string会成为nil。 就不会出现paste的选项。
当 ARC 环境下
class_addMethod([self class], @selector(resolveThisMethodDynamically), (IMP) myMethodIMP, “v@:”);
使用的时候@selector 须要使用super的class,否则会报错。
当MRC环境下
class_addMethod([EmptyClass class], @selector(sayHello2), (IMP)sayHello, “v@:”);
能够任意定义。可是系统会出现警告,忽略警告就能够。
将JSON的数据,转化为NSData, 放入Request的body中。 发送到服务器就是form-data格式。
1
2
3
4
5
6
7
|
BOOL hasBccCode = YES;
if ( nil == bccCodeStr
|| [bccCodeStr isKindOfClass:[NSNull class]]
|| [bccCodeStr isEqualToString:@""])
{
hasBccCode = NO;
}
|
若是进行非空判断和类型判断时,须要新进行类型判断,再进行非空判断,否则会crash。
能够在调用UIAlertView 以前进行键盘是否已经隐藏的判断。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
@property (nonatomic, assign) BOOL hasShowdKeyboard;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showKeyboard)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dismissKeyboard)
name:UIKeyboardDidHideNotification
object:nil];
- (void)showKeyboard
{
self.hasShowdKeyboard = YES;
}
- (void)dismissKeyboard
{
self.hasShowdKeyboard = NO;
}
while ( self.hasShowdKeyboard )
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
UIAlertView* alerview = [[UIAlertView alloc] initWithTitle:@"" message:@"取消修改?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"肯定", nil];
[alerview show];
|
模拟器默认的配置种没有“小地球”,只能输入英文。加入中文方法以下:
选择Settings—>General–>Keyboard–>International KeyBoards–>Add New Keyboard–>Chinese Simplified(PinYin) 即咱们通常用的简体中文拼音输入法,配置好后,再输入文字时,点击弹出键盘上的“小地球”就能够输入中文了。
若是不行,能够长按“小地球”选择中文。
phone 的键盘类型:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
- (IBAction)changeImages:(id)sender
{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:_parentView cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_parentView cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_parentView cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_parentView cache:YES];
NSInteger purple = [[_parentView subviews] indexOfObject:self.image1];
NSInteger maroon = [[_parentView subviews] indexOfObject:self.image2];
[_parentView exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
|
1
2
3
|
[[HXSLocationManager sharedManager] addObserver:self
forKeyPath:@"currentBoxEntry.boxCodeStr"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionOld context:nil];
|
在实现的类self中,进行[HXSLocationManager sharedManager]类中的变量@“currentBoxEntry.boxCodeStr” 监听。
在iOS9 中,若是进行animateWithDuration 时,view被release 那么会引发crash。
1
2
3
4
5
6
7
|
[UIView animateWithDuration:0.25f animations:^{
self.frame = selfFrame;
} completion:^(BOOL finished) {
if (finished) {
[super removeFromSuperview];
}
}];
|
会crash。
1
2
3
4
5
6
7
8
9
|
[UIView animateWithDuration:0.25f
delay:0
usingSpringWithDamping:1.0
initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear
animations:^{
self.frame = selfFrame;
} completion:^(BOOL finished) {
[super removeFromSuperview];
}];
|
不会Crash。
iPTV项目中在删除影片时,URL中需传送用户名与影片ID两个参数。当用户名中带中文字符时,删除失败。
以前测试时,手机号绑定的用户名是英文或数字。换了手机号测试时才发现这个问题。
对于URL中有中文字符的状况,需对URL进行编码转换。
1
|
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
虽然在Xcode能够看到jpg的图片,可是在加载的时候会失败。
错误为 Could not load the “ReversalImage1” image referenced from a nib in the bun
必须使用PNG的图片。
若是须要使用JPG 须要添加后缀
1
|
[UIImage imageNamed:@"myImage.jpg"];
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, [window center].x, [window center].y);
CGContextConcatCTM(context, [window transform]);
CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
[[window layer] renderInContext:context];
CGContextRestoreGState(context);
}
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
这个[CLLocationManager locationServicesEnabled]检测的是整个iOS系统的位置服务开关,没法检测当前应用是否被关闭。经过
1
2
3
4
5
6
|
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
[self locationManager:self.locationManager didUpdateLocations:nil];
} else { // the user has closed this function
[self.locationManager startUpdatingLocation];
}
|
CLAuthorizationStatus来判断是否能够访问GPS
text 的大小必须 大于0 小于 10k
image 必须 小于 64k
url 必须 大于 0k
通常使用SDWebImage 进行图片的显示和缓存,通常缓存的内容比较多了就须要进行清空缓存
清除SDWebImage的内存和硬盘时,能够同时清除session 和 cookie的缓存。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// 清理内存
[[SDImageCache sharedImageCache] clearMemory];
// 清理webview 缓存
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config.URLCache removeAllCachedResponses];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// 清理硬盘
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.tableView reloadData];
}];
|
当tableview的类型为 plain的时候,header View 就会停留在最上面。
当类型为 group的时候,header view 就会跟随tableview 一块儿滚动了。
在xib 或 storyboard 中能够进行tabBar的设置
其中badge 是自带的在图标上添加一个角标。
1. self.navigationItem.title 设置navigation的title 须要用这个进行设置。
2. self.title 在tab bar的主VC 中,进行设置self.title 会致使navigation 的title 和 tab bar的title一块儿被修改。
添加这两行代码:
1
2
|
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
|
顶部的阴影是在UIWindow上的,因此不能简单的设置就去除。
设置horizontal的值,表示出现内容很长的时候,优先压缩这个UIKit。
使用AFNetworking 时, 使用
1
2
3
4
|
AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init];
response.removesKeysWithNullValues = YES;
_sharedClient.responseSerializer = response;
|
这个参数 removesKeysWithNullValues 能够将null的值删除,那么就Value为nil了
// END