本篇文章記錄了iOS開發(fā)零碎知識點锐锣,簡單又實用!
代碼寫了這么多著洼,但是總是有些知識點在真正需要用到的時候卻遺忘了樟遣,一直想整理這塊知識,最近又總是在趕項目身笤,不管再忙豹悬,這塊總是要整理起來。
修改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];
擴大UIButton點擊區(qū)域
當(dāng)UI設(shè)計圖上的給出按鈕尺寸較小瞻佛,我們將對應(yīng)的資源文件放入UIButton中,在真機調(diào)試中會發(fā)現(xiàn)難以點到按鈕。這時候可以通過繼承UIButton莹弊,重寫pointInside方法涤久,使得按鈕事件響應(yīng)不夠我們設(shè)置的最小區(qū)域的自動擴大到我們的設(shè)置的最小區(qū)域涡尘。
.h定義我們設(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)
子視圖超出父視圖考抄,子視圖點擊事件不響應(yīng)细疚。一般子視圖超出父視圖,子視圖點擊等事件是不響應(yīng)的川梅,因為事件的傳遞鏈不會傳到超出父視圖的視圖上面疯兼,需要我們用``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;`吧彪,因為`UIScrollView`默認(rèn)會進(jìn)行裁剪待侵,會導(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)相機拍照功能
1、將使用照片改成保存至相冊赡艰;
2售淡、監(jiān)聽拍照按鈕點擊事件;
3慷垮、監(jiā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:@"保存至相冊" 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;
//給拍照加點擊事件
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;的作用是防止這層視圖的點擊事件影響系統(tǒng)的使用照片按鈕的點擊事件楔壤;在這里給拍照按鈕和重拍按鈕添加了點擊事件,既滿足了自己需要做的事情惯驼,又不影響系統(tǒng)對這兩個按鈕的點擊事件蹲嚣。
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ù)更新中。祟牲。隙畜。