iOS开发经验总结

iOS开发经验总结,更新中…

iPhone Size






































手机型号屏幕尺寸
iPhone 4/4s320 480
iPhone 6/6s 7 8375 667
iPhone 6plus/6s plus/7 plus/8 plus414 736
iPhone X/Xs375 812
iPhone XR276 598
iPhone Xs Max414 896

给navigation Bar 设置 title 颜色

1
2
3
UIColor *whiteColor = [UIColor whiteColor];
NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dic];

如何把一个CGPoint存入数组里

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

可以用NSValue进行基础数据的保存,用这个方法更加清晰明确。

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支持泛型了,可以用

*> *array```来保存。
1
2

## UIColor 获取 RGB 值

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

## 修改textField的placeholder的字体颜色、大小

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
2

推荐 使用attributedString进行设置.

NSString string = @”美丽新世界”;
NSMutableAttributedString
attributedString = [[NSMutableAttributedString alloc] initWithString:string];

[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor redColor]
                         range:NSMakeRange(0, [string length])];

[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont systemFontOfSize:16]
                         range:NSMakeRange(0, [string length])];

self.textField.attributedPlaceholder = attributedString;
1
2

## 两点之间的距离

static inline CGFloat CGPointDistanceBetweenTwoPoints(CGPoint point1, CGPoint point2) { CGFloat dx = point2.x - point1.x; CGFloat dy = point2.y - point1.y; return sqrt(dxdx + dydy);}

1
2
3
4

## iOS开发-关闭/收起键盘方法总结

1.点击Return按扭时收起键盘

  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    return [textField resignFirstResponder];
    }
    1
    2

    2.点击背景View收起键盘

[self.view endEditing:YES];

1
2

3.你可以在任何地方加上这句话,可以用来统一收起键盘

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

1
2
3
4
5
6
7
8

## 在使用 ImagesQA.xcassets 时需要注意

将图片直接拖入image到ImagesQA.xcassets中时,图片的名字会保留。 这个时候如果图片的名字过长,那么这个名字会存入到ImagesQA.xcassets中,名字过长会引起SourceTree判断异常。

## UIPickerView 判断开始选择到选择结束

开始选择的,需要在继承UiPickerView,创建一个子类,在子类中重载

  • (UIView)hitTest:(CGPoint)point withEvent:(UIEvent)event

    1
    2

    当```[super hitTest:point withEvent:event]```返回不是nil的时候,说明是点击中UIPickerView中了。 结束选择的, 实现UIPickerView的delegate方法
  • (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

    1
    2
    3
    4

    当调用这个方法的时候,说明选择已经结束了。

    ## iOS模拟器 键盘事件

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];

1
2
3
4

进行键盘事件的获取。那么在此情景下将不会调用```- (void)keyboardWillHide. ```因为没有键盘的隐藏和显示。

## 线程中更新 UILabel的text

[self.label1 performSelectorOnMainThread:@selector(setText:) withObject:textDisplay
waitUntilDone:YES];
```

label1 为UILabel,当在子线程中,需要进行text的更新的时候,可以使用这个方法来更新。 其他的UIView 也都是一样的。

使用UIScrollViewKeyboardDismissMode实现了Message app的行为