本篇文章記錄了iOS開發(fā)零碎知識(shí)點(diǎn),簡(jiǎn)單又實(shí)用!
代碼寫了這么多,但是總是有些知識(shí)點(diǎn)在真正需要用到的時(shí)候卻遺忘了,一直想整理這塊知識(shí),最近又總是在趕項(xiàng)目,不管再忙,這塊總是要整理起來。
修改Cell分割線距離
修改UITableviewCell的分割線距離通常需要修改separatorInset屬性的top, left, bottom, right:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
? ? [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
? ? [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
? ? [cell setPreservesSuperviewLayoutMargins:NO];
}
}
去掉Cell的分割線
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
取消Cell的選中效果
myTableView.selectionStyle = UITableViewCellSelectionStyleNone;
將漢字轉(zhuǎn)換為拼音
可以把漢字字符串轉(zhuǎn)換成拼音,并且支持是否在拼音間插入空格
- (NSString*)chineseToPinyin:(NSString*)chinese withSpace:(BOOL)withSpace {
? ? if(chinese) {
? ? ? ? CFStringRefhanzi = (__bridgeCFStringRef)chinese;
? ? ? ? CFMutableStringRefstring =CFStringCreateMutableCopy(NULL,0, hanzi);
? ? ? ? CFStringTransform(string,NULL, kCFStringTransformMandarinLatin,NO);
? ? ? ? CFStringTransform(string,NULL, kCFStringTransformStripDiacritics,NO);
? ? ? ? NSString*pinyin = (NSString*)CFBridgingRelease(string);
? ? ? ? if(!withSpace) {
? ? ? ? ? ? pinyin = [pinyin stringByReplacingOccurrencesOfString:@" "withString:@""];
? ? ? ? }
? ? return pinyin;
? ? }
return nil;
}
重置self.navigationController.viewControllers
NSArray*vcs = self.navigationController.viewControllers;
NSMutableArray*array = [NSMutableArrayarray];
for (inti =0; i < vcs.count; i++) {
UIViewController*temp = [vcsobjectAtIndex:i];
if (![tempisKindOfClass:NSClassFromString(viewControllersName)]) {
[arrayaddObject:temp];
}
}
[self.navigationControllersetViewControllers:arrayanimated:YES];
擴(kuò)大UIButton點(diǎn)擊區(qū)域
當(dāng)UI設(shè)計(jì)圖上的給出按鈕尺寸較小,我們將對(duì)應(yīng)的資源文件放入U(xiǎn)IButton中,在真機(jī)調(diào)試中會(huì)發(fā)現(xiàn)難以點(diǎn)到按鈕。這時(shí)候可以通過繼承UIButton,重寫pointInside方法,使得按鈕事件響應(yīng)不夠我們?cè)O(shè)置的最小區(qū)域的自動(dòng)擴(kuò)大到我們的設(shè)置的最小區(qū)域。
.h定義我們?cè)O(shè)置的最小響應(yīng)區(qū)域大小
/***? 事件響應(yīng)最小區(qū)域大小(小于此區(qū)域則放大,否則保持原大小不變,不賦值保持原大小不變)
*/
@property(nonatomic,assign)CGSizeeventFrame;
.m重寫pointInside方法
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event{
CGRectbounds =self.bounds;
CGFloatwidthExtra = MAX(self.eventFrame.width- bounds.size.width,0);
CGFloatheightExtra = MAX(self.eventFrame.width- bounds.size.height,0);
bounds =CGRectInset(bounds, -0.5* widthExtra, -0.5* heightExtra);
returnCGRectContainsPoint(bounds, point);
}
判斷非空字符串
+ (BOOL)isEmptyString:(NSString *)string {
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
設(shè)置抗壓縮-抗拉伸
[self.textFiledsetContentHuggingPriority:UILayoutPriorityDefaultLowforAxis:UILayoutConstraintAxisHorizontal];[self.textFiledsetContentCompressionResistancePriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];[self.codeImageViewsetContentHuggingPriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];[self.codeImageViewsetContentCompressionResistancePriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];
首次進(jìn)入某一功能模塊判斷
+ (BOOL)isFirstEnterNewModule{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
return NO;
}
視圖過大不響應(yīng)
子視圖超出父視圖,子視圖點(diǎn)擊事件不響應(yīng)。一般子視圖超出父視圖,子視圖點(diǎn)擊等事件是不響應(yīng)的,因?yàn)槭录膫鬟f鏈不會(huì)傳到超出父視圖的視圖上面,需要我們用``hitTest:withEvent:``處理下。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
? ? CGPoint hitPoint = [self.cardView.dayRateHelp convertPoint:point fromView:self];
? ? if ([self.cardView.dayRateHelp pointInside:hitPoint withEvent:event])
? ? return self.cardView.dayRateHelp;
? ? return [super hitTest:point withEvent:event];
}
注意:如果父視圖是UIScrollView,需要設(shè)置`self.bgScrollView.clipsToBounds = NO;`,因?yàn)閌UIScrollView`默認(rèn)會(huì)進(jìn)行裁剪,會(huì)導(dǎo)致超出的部分沒有了。
修改holder
修改UITextField的Placeholder的文字顏色和大小。這里我們使用kvc設(shè)置UITextField的私有屬性。
[textField setValue:placeholderLabelTextColor forKeyPath:@"_placeholderLabel.textColor"];[textField setValue:[UIFont systemFontOfSize:placeholderLabelFont] forKeyPath:@"_placeholderLabel.font"];
修改UIPageControl圖片
修改UIPageControl的選中圖片和默認(rèn)圖片。默認(rèn)也是不允許修改的,需要用到kvc設(shè)置。
[self.pageControl setValue:currentImage forKey:@"_currentPageImage"];
[self.pageControl setValue:pageImage forKey:@"_pageImage"];
打電話
NSString *phoneNum = @"";
NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];
if ( !phoneCallWebView ) {
? ? phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
修改系統(tǒng)相機(jī)拍照功能
1、將使用照片改成保存至相冊(cè);
2、監(jiān)聽拍照按鈕點(diǎn)擊事件;
3、監(jiān)聽重拍按鈕點(diǎn)擊事件;
4、在拍照里面添加自定義view放到cameraOverlayView上。
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self addSomeElements:viewController];
}
- (UIView *)findView:(UIView *)aView withName:(NSString *)name {
? ? Class cl = [aView class];
? ? NSString *desc = [cl description];
? ? if ([name isEqualToString:desc]) return aView;
? ? for (UIView *view in aView.subviews) {
? ? ? ? Class cll = [view class];
? ? ? ? NSString *stringl = [cll description];
? ? ? ? if ([stringl isEqualToString:name]) {
? ? ? ? ? ? return view;
? ? ? ? }
? ? }
? ? return nil;
}
- (void)addSomeElements:(UIViewController *)viewController {
? ? UIView *PLCropOverlay = [self findView:viewController.view withName:@"PLCropOverlay"];
? ? UIView *PLCropOverlayBottomBar = [self findView:PLCropOverlay withName:@"PLCropOverlayBottomBar"];
? ? UIView *PLCropOverlayPreviewBottomBar = [self findView:PLCropOverlayBottomBar withName:@"PLCropOverlayPreviewBottomBar"];
? ? UIButton *userButton = [PLCropOverlayPreviewBottomBar.subviews objectAtIndex:2];
? ? UIButton *viewbtn = [[UIButton alloc] init];
? ? [viewbtn setTitle:@"保存至相冊(cè)" forState:UIControlStateNormal];
? ? [viewbtn setTitleColor:[UIColor whiteColor] forState:0];
? ? viewbtn.backgroundColor = RGB(19, 20, 21);
? ? [userButton addSubview:viewbtn];
? ? [viewbtn mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? make.trailing.equalTo(userButton.mas_trailing);
? ? ? ? make.centerY.equalTo(userButton);
}];
viewbtn.userInteractionEnabled = NO;
//給拍照加點(diǎn)擊事件
UIView *CMKBottomBar = [self findView:viewController.view withName:@"CMKBottomBar"];
UIButton *CMKShutterButton = (UIButton *) [self findView:CMKBottomBar withName:@"CMKShutterButton"];
[CMKShutterButton addTarget:self action:@selector(shutterButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//監(jiān)聽重拍
UIButton *resetButton = [PLCropOverlayPreviewBottomBar.subviews objectAtIndex:0];
[resetButton addTarget:self action:@selector(resetButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
注意:viewbtn.userInteractionEnabled = NO;的作用是防止這層視圖的點(diǎn)擊事件影響系統(tǒng)的使用照片按鈕的點(diǎn)擊事件;在這里給拍照按鈕和重拍按鈕添加了點(diǎn)擊事件,既滿足了自己需要做的事情,又不影響系統(tǒng)對(duì)這兩個(gè)按鈕的點(diǎn)擊事件。
iOS10 UIPickerView線條不顯示
#define IOS10_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >=10.0)
if (IOS10_OR_LATER) {
? ? for (UIView*separatorLine in pickerView.subviews) {
? ? ? ? if (separatorLine.frame.size.height<1) {
? ? ? ? separatorLine.backgroundColor= [UIColorwd_colorWithd0d0d0];
? ? ? ? }
? ? }
}
持續(xù)更新中。。。