iOS开发稀碎小技巧一

xiaoxiao2021-02-28  43

原写于简书,现转在记录

之前一直在印象笔记记录一些iOS开发遇到的小技巧,难度不一,回头大概整理记录一下发出来,兴许也会帮到别人;-)

1.返回上两级页面:

NSInteger index = [[self.navigationController viewControllers]indexOfObject:self]; [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index - 2] animated:YES];

返回另一个tabbar首页:

self.tabBarController.selectedIndex = [SFTabbarController communityViewControllerIndex]; [self.navigationController popToRootViewControllerAnimated:YES];

2.打开设置

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

3.self.tableView调用reloadData方法时,若没有数据,tableview会置顶滚到头,而且tableview的contentSize会缩小至tableheader的高度(若有tableHeader的话,因为无数据需要在table上展示placeHolder的话,故需注意)

- (void)reloadData; reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks.

解决方法: CGPoint offset = self.tableView.contentOffset; [self.tableView reloadData]; if (self.dataArray.count == 0) { self.tableView.contentOffset = offset; }

4.设置了 cell.selectionStyle = UITableViewCellSelectionStyleNone;后,highlightedTextColor会失效!

cell.textLabel.textColor =SFColorHexString(SFLightGrayColor); cell.textLabel.highlightedTextColor =SFColorHexString(SFBlackColor);

5.selectedBackgroundView 不同于contentView

cell.selectedBackgroundView.backgroundColor = SFColorHexString(SFWhiteColor);

6.初始化一个全局变量或static变量时,只能用常量赋值,不能用变量赋值!

7.设置center中心对齐注意view和父view的center不一样,参考坐标系bounds不同

_imageView.centerX = _contentBgView.centerX; //注意以上代码并不会中心X对齐,有可能_imageView和_contentBgView的父view不同; //那就这么设置,让这俩viewX对齐: _imageView.centerX = _contentBgView.width * 0.5;

8.XIB创建button时,设置为custom,一些选项才会生效(Highlighted Adjusts Image是点击button关闭highted变色)

9.UITextField 在xib中创建的无边框style,点击编辑,文字会下移的bug!

1:self.nickTF.clearButtonMode = UITextFieldViewModeWhileEditing;(这样后面会带一个删除✘)2:xib时选择带边框的,线拉出属性,再在viewDidLoad里设置成无边框样式,这个完美解决;

10.DEBUG

#ifdef DEBUG // 调试状态, 打开LOG功能 #define SFString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent //打印出所在文件名,所在行,堆栈地址 #define SFLog(...) printf("%s: %p (line = %d): %s\n\n", [SFString UTF8String] , &self, __LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]); //#else //#define SFLog(s, ...) NSLog(@"<%@: %p (line = %d)> %@", self.class, self, __LINE__,[NSString stringWithFormat:(s),##__VA_ARGS__]) //#endif #else // 发布状态, 关闭LOG功能 #define SFLog(s, ...) #endif 系统宏介绍: __LINE__:宏在预编译时会替换成当前的行号 __FUNCTION__:宏在预编译时会替换成当前的函数名称 __VA_ARGS__:简单的说,就是将左边…的内容替换进来

11.获得字体大小

_dateLabel.font.pointSize

12.错误提醒、报错

//NSASSert断言: //condition不成立时,执行后面 NSAssert(condition, @"Argument must be non-nil”); NSAssert(0, @"Argument must be non-nil”);//一定在此断掉 [NSException raise…: [[NSException exceptionWithName:@"YZDisplayViewControllerException" reason:@"字体放大效果和角标不能同时使用。" userInfo:nil] raise];

13.在.m中的匿名类别中定义的匿名属性:

@interface ViewController () @property (nonatomic, strong) NSString *string; @end 可以在别的地方用setValue:...forKey:...的方法赋值上 ViewController *vc = [[ViewController alloc]init]; [vc setValue:@"哈哈哈" forKey:@"string"]; 在当前类的类方法中怎么访问匿名属性: ViewController *vc = [[ViewController alloc]init]; 法1: [vc valueForKey:@“string”] = @“哈哈哈”; 法2: vc -> _string = @“哈哈哈”; imageView添加切换动画 [_backgroundImgV sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:LoadNameImage(@"cityBg_default") completed:nil]; CATransition *transition = [CATransition animation]; transition.duration = 0.35f; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; transition.type = kCATransitionFade; [_backgroundImgV.layer addAnimation:transition forKey:nil];

15.writeToFile:atomically:

//第二个参数的意思是:如果为YES则保证文件的写入原子性,就是说会先创建一个临时文件,直到文件内容写入成功再导入到目标文件里.如果为NO,则直接写入目标文件里. - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

16.代理一般设置成weak修饰,但是苹果把NSURLSession的如下方法其中的delegate设计成strong属性的,目的是为了防止被释放掉。

+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(nullable id <NSURLSessionDelegate>)delegate delegateQueue:(nullable NSOperationQueue *)queue;

The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session.

TextField 的placeholder字体颜色和大小 修改(运行时查看属性,KVC神器) textField.placeholder = @"xxxx”; //光标颜色 textField.tintColor = [UIColor redColor]; [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"]; //------设置placeholder的大小后,如果不是系统默认大小,会出现垂直不居中的情况,解决如下 NSMutableParagraphStyle *style = [textField.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy]; style.minimumLineHeight = textField.font.lineHeight - (textField.font.lineHeight - [UIFont systemFontOfSize:13.0f].lineHeight) / 2.0; //[UIFont systemFontOfSize:13.0f]是设置的placeholder的字体 textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSParagraphStyleAttributeName : style}]; //------如果输入文字不居中,placeholder不居中,重写系统方法 -(CGRect)editingRectForBounds:(CGRect)bounds; -(CGRect)placeholderRectForBounds:(CGRect)bounds;

18.点击cell 此cell置顶,置底,置中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section] animated:YES scrollPosition:UITableViewScrollPositionTop]; }

19.创建图片 并且拉伸图片

UIImage * barImage = [[UIImage imageNamed:@"navigationbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch ]; //UIEdgeInsets 距离内边距上下左右的距离 //枚举值:UIImageResizingModeStretch拉伸 UIImageResizingModeTile平铺(默认)

20.类方法中不能用self. (这样会访问set,get),类方法里不能调用实例方法;但是可以创建对象来访问实例方法 1,类方法可以调用类方法。 2,类方法不可以调用实例方法,但是类方法可以通过创建对象来访问实例方法。 3,类方法不可以使用实例变量。类方法可以使用self,因为self不是实例变量。 4,类方法作为消息,可以被发送到类或者对象里面去(实际上,就是可以通过类或者对象调用类方法的意思)。 注意点二:self的规则 1,实例方法里面的self,是对象的首地址。 2,类方法里面的self,是Class.

#import "Dog.h" @implementation Dog +(void)method1 { Dog *dog = [[Dog alloc]init]; dog.name = @"ss"; object_getClassName(dog); NSString *classStr = NSStringFromClass(self); [[self class] method2]; } @end

21.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath2 { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } // 不加此句时,在二级栏目点击返回时,此行会由选中状态慢慢变成非选中状态。 // 加上此句,返回时直接就是非选中状态。

22. 当未使用 ARC 时,你的 dealloc 实现必须把调用父类的实现作为最后一条指令。(隐含的意思就是,使用 ARC 时不能调用父类的实现); 但有些时候会发现控制器出栈的时候不会调用dealloc方法,归根结底,是因为当前控制器被某个对象强引用并“握住”了,控制器的引用计数不为0,系统无法帮你释放这部分内存。 控制器被强引用的原因: 1.block块使用不当。因为block会对方法中的变量自动retain一次。请检查控制器中block代码。 2.NSTimer没有销毁。在viewWillDisappear之前需要把控制器用到的NSTimer销毁,具体操作 NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:nil userInfo:nil repeats:YES]; [timer invalidate]; // 销毁timer timer = nil; // 置nil 3.控制器中的代理属性未设置成assign或weak。 23. removeFromSuperview会破坏cellForRowAtIndexPath的重用机制,若不是自定义的cell,就需要利用tag值给cell的创建subviews或换值来利用重用机制;若是自定义的cell就在cell的init方法里 addSubviews。

24.问题1:当tableview style设置为plain时,每个section的header会悬浮在屏幕最上面。让header也跟随tableview一起上下滚动,不停在屏幕最上的部分:

//去掉UItableview headerview黏性 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.myTableView) { CGFloat sectionHeaderHeight = YOUR_HEIGHT; if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0); } } }

问题2:当tableview style设置为ground时,每个section的header会跟随tableview一起上下滚动,有时会发现footer或header总会有间距,这时就要设置:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.01;//不能为0;不能只重写任何一个代理方法,必须两个都设置为很小的数。 } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.01; }

25.CoreGraphic框架中定义好的宏,可判断

CGPointEqualToPoint CGSizeEqualToSize(size, size) == YES

26. 在.m中( )中有名字就是类别,没有就是Extention(匿名类别)

@interface ViewController ( ) { NSTimer *_timer; } @end

27.LaunchScreen.storyboard上只能放一些静态图片,并且不能关联自定义的ViewController; 如果要动态设置启动图,或者启动动画,需要在APPDelegate中的Finish方法中,创建View,把图片加载到View上:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *){}

28.UITableViewController默认的会在viewWillAppear的时候,清空所有选中cell。 设置self.clearsSelectionOnViewWillAppear = NO,来禁用该功能, 在viewDidAppear中调用UIScrollView的flashScrollIndicators方法让滚动条闪动一次,提示用户该控件是可以滑动的。

29. 原来当程序第一次调用self.view的时候,viewDidLoad方法就会被执行,而不一定非要等到init之后willAppear之前。这给我们敲响了警钟,这样的代码就隐藏了问题:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.view.backgroundColor = [UIColor yellowColor]; aInstanceVariable_= 0; // Custom initialization of an instance variable } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. aInstanceVariable_ = 10086; }

这段代码执行完后的aInstanceVariable_是0而不是10086,可能会为一些bug深深地埋下一颗种子。

30.任务栏小菊花 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

转载请注明原文地址: https://www.6miu.com/read-50551.html

最新回复(0)