博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective-C开发使用技巧总结
阅读量:5960 次
发布时间:2019-06-19

本文共 6959 字,大约阅读时间需要 23 分钟。

1.tableView 分割线左边短15像素问题

首先在viewDidLoad方法加入以下代码:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {        [self.tableView setSeparatorInset:UIEdgeInsetsZero];    }    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {                [self.tableView setLayoutMargins:UIEdgeInsetsZero];}复制代码

然后重写willDisplayCell方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{       if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {                    [cell setSeparatorInset:UIEdgeInsetsZero];        }        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {                     [cell setLayoutMargins:UIEdgeInsetsZero];        }}复制代码

2. 简单的获取当前时间

// CFAbsoluteTime其实就是doubleCFAbsoluteTime time = CFAbsoluteTimeGetCurrent();复制代码

3.程序直接退出

exit(0);复制代码

4.让一个视图始终在最前面

view.layer.zPosition = 999;复制代码

5.判断一个view是不是指定view的子视图

BOOL isChildView =  [childView isDescendantOfView:parentView];复制代码

6.UIViewController中的几个重要方法

* alloc 创建对象,分配空间* init (initWithNibName) 初始化对象,初始化数据* loadView 从nib载入视图 ,除非你没有使用xib文件创建视图* viewDidLoad 载入完成,可以进行自定义数据以及动态创建其他控件* viewWillAppear视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了* viewDidAppear 视图已在屏幕上渲染完成* viewWillDisappear 视图将被从屏幕上移除之前执行* viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图了* dealloc 视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放.* viewVillUnload- 当内存过低,即将释放时调用;* viewDidUnload-当内存过低,释放一些不需要的视图时调用。复制代码

7.视图中坐标转换

// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;// 将rect从view中转换到当前视图中,返回在当前视图中的rect- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;*例把UITableViewCell中的subview(btn)的frame转换到controllerA中// controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button// 在controllerA中实现:CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];或CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];// 此rc为btn在controllerA中的rect或当已知btn时:CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];或CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];复制代码

8. 利用宏在扩展类添加属性

#define ASSOCIATED(propertyName, setter, type, objc_AssociationPolicy)\- (type)propertyName {\return objc_getAssociatedObject(self, _cmd);\}\\- (void)setter:(type)object\{\objc_setAssociatedObject(self, @selector(propertyName), object, objc_AssociationPolicy);\}复制代码

9.汉字转拼音

- (NSString *)stringToPinyin{    if ([self length] > 0) {        NSMutableString *ms = [[NSMutableString alloc] initWithString:self];        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {        }        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {            //NSLog(@"pinyin: %@", ms);            return ms;        }    }    return self;}复制代码

10.给空间指定位置添加圆角

- (void)viewAddBezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii{    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:cornerRadii];    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];    maskLayer.frame = self.bounds;    maskLayer.path = maskPath.CGPath;    self.layer.mask = maskLayer;}复制代码

11.已某个view截屏并生成Image

- (UIImage*)viewShot{    UIGraphicsBeginImageContext(self.bounds.size);    [self.layer renderInContext:UIGraphicsGetCurrentContext()];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}复制代码

12.避免同时点击多个Button

第一种全局方式:在AppDelegate中添加 [[UIButton appearance] setExclusiveTouch:YES];第二种指定方式:button.exclusiveTouch = YES;复制代码

13.Debug打印日志

#pragma mark - 打印日志#ifdef DEBUG                    // 调试状态 打开LOG功能#define NSLog(FORMAT, ...) fprintf(stdout,"[NSLOG] [类名:%s : 第%d行的NSLog] %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);NSLogger//#define NSLog(...) LogMessageF( __FILE__,__LINE__,__FUNCTION__, NULL, 0, __VA_ARGS__);#else                           // 发布状态// 关闭LOG功能#define NSLog(FORMAT, ...)#endif复制代码

14.适配iOS 11

if (@available(iOS 11.0, *)){        //        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];        [UITableView appearance].estimatedRowHeight = 0;        [UITableView appearance].estimatedSectionHeaderHeight = 0;        [UITableView appearance].estimatedSectionFooterHeight = 0;    }复制代码

15.设置tabbar

/** *  更多TabBar自定义设置:比如:tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等 */- (void)customizeTabBarAppearance:(UITabBarController *)tabBarController {    // Customize UITabBar height    // 自定义 TabBar 高度    tabBarController.tabBarHeight = TG_TANGO_TABBAR_HEIGHT;        // set the text color for unselected state    // 普通状态下的文字属性    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];        // set the text color for selected state    // 选中状态下的文字属性    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];    selectedAttrs[NSForegroundColorAttributeName] = TangoGreen;        // set the text Attributes    // 设置文字属性    UITabBarItem *tabBar = [UITabBarItem appearance];    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];        // Set the dark color to selected tab (the dimmed background)    // TabBarItem选中后的背景颜色    // [self customizeTabBarSelectionIndicatorImage];        // update TabBar when TabBarItem width did update    // If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight,    // remove the comment '//'    // 如果你的App需要支持横竖屏,请使用该方法移除注释 '//'    // [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];        // set the bar shadow image    // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];    [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];        // set the bar background image    UITabBar *tabBarAppearance = [UITabBar appearance];    // 设置背景图片    if (IS_IPHONE_X) {        //换个图片            }else{        [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background"]];    }        // remove the bar system shadow image    // 去除 TabBar 自带的顶部阴影    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];}复制代码

转载于:https://juejin.im/post/5a52d498518825735300716e

你可能感兴趣的文章