41、适配iOS11
if (@available(ios 11.0 ,*)) {
UIScrollView.appearance .contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever
UITableView.appearance .estimatedRowHeight = 0
UITableView.appearance .estimatedSectionFooterHeight = 0
UITableView.appearance .estimatedSectionHeaderHeight = 0
}
42、查看系统所有字体
for (id familyName in [UIFont familyNames]) {
NSLog (@"%@" , familyName);
for (id fontName in [UIFont fontNamesForFamilyName:familyName]) NSLog (@" %@" , fontName);
}
43、获取随机数
NSInteger i = arc4random();
44、获取随机数小数(0-1之间)
#define ARC4RANDOM_MAX 0x100000000
double val = ((double )arc4random() / ARC4RANDOM_MAX);
45、保存UIImage到本地
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES );
NSString *filePath = [[paths objectAtIndex:0 ] stringByAppendingPathComponent:@"Image.png" ];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES ];
46、键盘栏上方增加工具栏
UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector (doneClicked:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil ]];
txtField.inputAccessoryView = keyboardDoneButtonView;
47、UICollectionView自动滚动到某行
-(void )viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self .collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO ];
}
48、让导航控制器pop回指定的控制器
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:[self .navigationController viewControllers]];
for (UIViewController *aViewController in allViewControllers) {
if ([aViewController isKindOfClass:[RequiredViewController class]]) {
[self .navigationController popToViewController:aViewController animated:NO ];
}
}
49、判断字典中是否包含某个key值
if ([dic objectForKey:@"yourKey" ]) {
NSLog (@"有这个值" );
} else {
NSLog (@"没有这个值" );
}
50、选中textView所有文本
[self.textView setSelectedTextRange:[self.textView textRangeFromPosition:self.textView .beginningOfDocument toPosition:self.textView .endOfDocument ]]
51、隐藏UITextView/UITextField光标
textField.tintColor = [UIColor clearColor];
52、当UITextView/UITextField中没有文字时,禁用回车键
textField.enablesReturnKeyAutomatically = YES;
53、获取随机UUID
NSString *result;
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 6.0 )
{
result = [[NSUUID UUID] UUIDString];
}
else
{
CFUUIDRef uuidRef = CFUUIDCreate(NULL );
CFStringRef uuid = CFUUIDCreateString(NULL , uuidRef);
CFRelease(uuidRef);
result = (__bridge_transfer NSString *)uuid;
}
54、修改UISearBar内部背景颜色
UITextField *textField = [_searchBar valueForKey:@"_searchField" ];
textField.backgroundColor = [UIColor redColor];
55、UITextView滚动到顶部
[self .textView scrollRangeToVisible:NSMakeRange(0 , 0 )];
[self .textView setContentOffset:CGPointZero animated:YES ];
56、通知监听APP生命周期
UIApplicationDidEnterBackgroundNotification 应用程序进入后台
UIApplicationWillEnterForegroundNotification 应用程序将要进入前台
UIApplicationDidFinishLaunchingNotification 应用程序完成启动
UIApplicationDidFinishLaunchingNotification 应用程序由挂起变的活跃
UIApplicationWillResignActiveNotification 应用程序挂起(有电话进来或者锁屏)
UIApplicationDidReceiveMemoryWarningNotification 应用程序收到内存警告
UIApplicationDidReceiveMemoryWarningNotification 应用程序终止(后台杀死、手机关机等)
UIApplicationSignificantTimeChangeNotification 当有重大时间改变(凌晨0点,设备时间被修改,时区改变等)
UIApplicationWillChangeStatusBarOrientationNotification 设备方向将要改变
UIApplicationDidChangeStatusBarOrientationNotification 设备方向改变
UIApplicationWillChangeStatusBarFrameNotification 设备状态栏frame将要改变
UIApplicationDidChangeStatusBarFrameNotification 设备状态栏frame改变
UIApplicationBackgroundRefreshStatusDidChangeNotification 应用程序在后台下载内容的状态发生变化
UIApplicationProtectedDataWillBecomeUnavailable 本地受保护的文件被锁定,无法访问
UIApplicationProtectedDataWillBecomeUnavailable 本地受保护的文件可用了
57、触摸事件类型
UIControlEventTouchCancel 取消控件当前触发的事件
UIControlEventTouchDown 点按下去的事件
UIControlEventTouchDownRepeat 重复的触动事件
UIControlEventTouchDragEnter 手指被拖动到控件的边界的事件
UIControlEventTouchDragExit 一个手指从控件内拖到外界的事件
UIControlEventTouchDragInside 手指在控件的边界内拖动的事件
UIControlEventTouchDragOutside 手指在控件边界之外被拖动的事件
UIControlEventTouchUpInside 手指处于控制范围内的触摸事件
UIControlEventTouchUpOutside 手指超出控制范围的控制中的触摸事件
58、设置UITextField光标位置
// textField需要设置的textField,index要设置的光标位置
- (void)cursorLocation:(UITextField *)textField index:(NSInteger)index
{
NSRange range = NSMakeRange(index, 0);
UITextPosition *start = [textField positionFromPosition:[textField beginningOfDocument] offset:range.location];
UITextPosition *end = [textField positionFromPosition:start offset:range.length];
[textField setSelectedTextRange:[textField textRangeFromPosition:start toPosition:end ]];
}
59、去除webView底部黑色
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO ];
for (UIView *v1 in [webView subviews])
{
if ([v1 isKindOfClass:[UIScrollView class]])
{
for (UIView *v2 in v1.subviews )
{
if ([v2 isKindOfClass:[UIImageView class]])
{
v2.hidden = YES ;
}
}
}
}
- (instancetype)initWithFrame:(CGRect )frame {
if (self = [super initWithFrame:frame]) {
self .delaysContentTouches = NO ;
}
return self ;
}
- (BOOL )touchesShouldCancelInContentView:(UIView *)view {
if ([view isKindOfClass:UIButton .class ]) {
return YES ;
}
return [super touchesShouldCancelInContentView:view];
}