iOS开发零碎知识点

本篇文章记录了iOS开发零碎知识点,简单又实用!

代码写了这么多,但是总是有些知识点在真正需要用到的时候却遗忘了,一直想整理这块知识,最近又总是在赶项目,不管再忙,这块总是要整理起来。


iOS开发零碎知识点_第1张图片
iOS开发零碎知识点

修改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;

将汉字转换为拼音

可以把汉字字符串转换成拼音,并且支持是否在拼音间插入空格

- (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点击区域

当UI设计图上的给出按钮尺寸较小,我们将对应的资源文件放入UIButton中,在真机调试中会发现难以点到按钮。这时候可以通过继承UIButton,重写pointInside方法,使得按钮事件响应不够我们设置的最小区域的自动扩大到我们的设置的最小区域。

.h定义我们设置的最小响应区域大小
/**

*  事件响应最小区域大小(小于此区域则放大,否则保持原大小不变,不赋值保持原大小不变)

*/

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

}

设置抗压缩-抗拉伸

[self.textFiledsetContentHuggingPriority:UILayoutPriorityDefaultLowforAxis:UILayoutConstraintAxisHorizontal];[self.textFiledsetContentCompressionResistancePriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];[self.codeImageViewsetContentHuggingPriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];[self.codeImageViewsetContentCompressionResistancePriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisHorizontal];

首次进入某一功能模块判断

+ (BOOL)isFirstEnterNewModule{

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]) {

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];

[[NSUserDefaults standardUserDefaults] synchronize];

return YES;

}

return NO;

}

视图过大不响应

子视图超出父视图,子视图点击事件不响应。一般子视图超出父视图,子视图点击等事件是不响应的,因为事件的传递链不会传到超出父视图的视图上面,需要我们用``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,需要设置`self.bgScrollView.clipsToBounds = NO;`,因为`UIScrollView`默认会进行裁剪,会导致超出的部分没有了。

修改holder

修改UITextField的Placeholder的文字颜色和大小。这里我们使用kvc设置UITextField的私有属性。

[textField setValue:placeholderLabelTextColor forKeyPath:@"_placeholderLabel.textColor"];[textField setValue:[UIFont systemFontOfSize:placeholderLabelFont] forKeyPath:@"_placeholderLabel.font"];

修改UIPageControl图片

修改UIPageControl的选中图片和默认图片。默认也是不允许修改的,需要用到kvc设置。

[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]];

修改系统相机拍照功能

1、将使用照片改成保存至相册;

2、监听拍照按钮点击事件;

3、监听重拍按钮点击事件;

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

//监听重拍

UIButton *resetButton = [PLCropOverlayPreviewBottomBar.subviews objectAtIndex:0];

[resetButton addTarget:self action:@selector(resetButtonClicked) forControlEvents:UIControlEventTouchUpInside];

}

注意:viewbtn.userInteractionEnabled = NO;的作用是防止这层视图的点击事件影响系统的使用照片按钮的点击事件;在这里给拍照按钮和重拍按钮添加了点击事件,既满足了自己需要做的事情,又不影响系统对这两个按钮的点击事件。

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

        }

    }

}

持续更新中。。。

你可能感兴趣的:(iOS开发零碎知识点)