之前一直在印象筆記記錄一些iOS開(kāi)發(fā)遇到的小技巧蕾总,難度不一戴尸,回頭大概整理記錄一下發(fā)出來(lái)粟焊,興許也會(huì)幫到別人;-)
1.返回上兩級(jí)頁(yè)面:
NSInteger index = [[self.navigationController viewControllers]indexOfObject:self];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index - 2] animated:YES];
返回另一個(gè)tabbar首頁(yè):
self.tabBarController.selectedIndex = [SFTabbarController communityViewControllerIndex];
[self.navigationController popToRootViewControllerAnimated:YES];
2.打開(kāi)設(shè)置
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
3.self.tableView調(diào)用reloadData方法時(shí),若沒(méi)有數(shù)據(jù)孙蒙,tableview會(huì)置頂滾到頭项棠,而且tableview的contentSize會(huì)縮小至tableheader的高度(若有tableHeader的話,因?yàn)闊o(wú)數(shù)據(jù)需要在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.設(shè)置了 cell.selectionStyle = UITableViewCellSelectionStyleNone;
后香追,highlightedTextColor
會(huì)失效!
cell.textLabel.textColor =SFColorHexString(SFLightGrayColor);
cell.textLabel.highlightedTextColor =SFColorHexString(SFBlackColor);
5.selectedBackgroundView 不同于contentView
cell.selectedBackgroundView.backgroundColor = SFColorHexString(SFWhiteColor);
6.初始化一個(gè)全局變量或static變量時(shí)坦胶,只能用常量賦值透典,不能用變量賦值!
7.設(shè)置center中心對(duì)齊注意view和父view的center不一樣顿苇,參考坐標(biāo)系bounds不同
_imageView.centerX = _contentBgView.centerX;
//注意以上代碼并不會(huì)中心X對(duì)齊峭咒,有可能_imageView和_contentBgView的父view不同;
//那就這么設(shè)置纪岁,讓這倆viewX對(duì)齊:
_imageView.centerX = _contentBgView.width * 0.5;
8.XIB創(chuàng)建button時(shí)凑队,設(shè)置為custom,一些選項(xiàng)才會(huì)生效(Highlighted Adjusts Image是點(diǎn)擊button關(guān)閉highted變色)
9.UITextField 在xib中創(chuàng)建的無(wú)邊框style幔翰,點(diǎn)擊編輯顽决,文字會(huì)下移的bug短条!
法1:self.nickTF.clearButtonMode = UITextFieldViewModeWhileEditing;(這樣后面會(huì)帶一個(gè)刪除?)
法2:xib時(shí)選擇帶邊框的,線拉出屬性,再在viewDidLoad里設(shè)置成無(wú)邊框樣式,這個(gè)完美解決才菠;
10.DEBUG
#ifdef DEBUG // 調(diào)試狀態(tài), 打開(kāi)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 // 發(fā)布狀態(tài), 關(guān)閉LOG功能
#define SFLog(s, ...)
#endif
系統(tǒng)宏介紹:
__LINE__:宏在預(yù)編譯時(shí)會(huì)替換成當(dāng)前的行號(hào)
__FUNCTION__:宏在預(yù)編譯時(shí)會(huì)替換成當(dāng)前的函數(shù)名稱(chēng)
__VA_ARGS__:簡(jiǎn)單的說(shuō)赋访,就是將左邊…的內(nèi)容替換進(jìn)來(lái)
11.獲得字體大小
_dateLabel.font.pointSize
12.錯(cuò)誤提醒可都、報(bào)錯(cuò)
//NSASSert斷言:
//condition不成立時(shí),執(zhí)行后面
NSAssert(condition, @"Argument must be non-nil”);
NSAssert(0, @"Argument must be non-nil”);//一定在此斷掉
[NSException raise…: [[NSException exceptionWithName:@"YZDisplayViewControllerException"
reason:@"字體放大效果和角標(biāo)不能同時(shí)使用蚓耽。"
userInfo:nil] raise];
13.在.m中的匿名類(lèi)別中定義的匿名屬性:
@interface ViewController ()
@property (nonatomic, strong) NSString *string;
@end
可以在別的地方用setValue:...forKey:...的方法賦值上
ViewController *vc = [[ViewController alloc]init];
[vc setValue:@"哈哈哈" forKey:@"string"];
在當(dāng)前類(lèi)的類(lèi)方法中怎么訪問(wèn)匿名屬性:
ViewController *vc = [[ViewController alloc]init];
法1:
[vc valueForKey:@“string”] = @“哈哈哈”;
法2:
vc -> _string = @“哈哈哈”;
- imageView添加切換動(dòng)畫(huà)
[_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:
//第二個(gè)參數(shù)的意思是:如果為YES則保證文件的寫(xiě)入原子性,就是說(shuō)會(huì)先創(chuàng)建一個(gè)臨時(shí)文件,直到文件內(nèi)容寫(xiě)入成功再導(dǎo)入到目標(biāo)文件里.如果為NO,則直接寫(xiě)入目標(biāo)文件里.
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
16.代理一般設(shè)置成weak修飾渠牲,但是蘋(píng)果把NSURLSession的如下方法其中的delegate設(shè)計(jì)成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字體顏色和大小 修改(運(yùn)行時(shí)查看屬性签杈,KVC神器)
textField.placeholder = @"xxxx”;
//光標(biāo)顏色
textField.tintColor = [UIColor redColor];
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
//------設(shè)置placeholder的大小后,如果不是系統(tǒng)默認(rèn)大小鼎兽,會(huì)出現(xiàn)垂直不居中的情況答姥,解決如下
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]是設(shè)置的placeholder的字體
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"請(qǐng)輸入密碼" attributes:@{NSParagraphStyleAttributeName : style}];
//------如果輸入文字不居中,placeholder不居中谚咬,重寫(xiě)系統(tǒng)方法
-(CGRect)editingRectForBounds:(CGRect)bounds;
-(CGRect)placeholderRectForBounds:(CGRect)bounds;
18.點(diǎn)擊cell 此cell置頂鹦付,置底,置中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
19.創(chuàng)建圖片 并且拉伸圖片
UIImage * barImage = [[UIImage imageNamed:@"navigationbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch
];
//UIEdgeInsets 距離內(nèi)邊距上下左右的距離
//枚舉值:UIImageResizingModeStretch拉伸 UIImageResizingModeTile平鋪(默認(rèn))
20.類(lèi)方法中不能用self. (這樣會(huì)訪問(wèn)set择卦,get)敲长,類(lèi)方法里不能調(diào)用實(shí)例方法;但是可以創(chuàng)建對(duì)象來(lái)訪問(wèn)實(shí)例方法
1秉继,類(lèi)方法可以調(diào)用類(lèi)方法祈噪。
2,類(lèi)方法不可以調(diào)用實(shí)例方法尚辑,但是類(lèi)方法可以通過(guò)創(chuàng)建對(duì)象來(lái)訪問(wèn)實(shí)例方法辑鲤。
3,類(lèi)方法不可以使用實(shí)例變量腌巾。類(lèi)方法可以使用self,因?yàn)閟elf不是實(shí)例變量铲觉。
4澈蝙,類(lèi)方法作為消息,可以被發(fā)送到類(lèi)或者對(duì)象里面去(實(shí)際上撵幽,就是可以通過(guò)類(lèi)或者對(duì)象調(diào)用類(lèi)方法的意思)灯荧。
注意點(diǎn)二:self的規(guī)則
1,實(shí)例方法里面的self盐杂,是對(duì)象的首地址逗载。
2哆窿,類(lèi)方法里面的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
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath2 {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// 不加此句時(shí)厉斟,在二級(jí)欄目點(diǎn)擊返回時(shí)挚躯,此行會(huì)由選中狀態(tài)慢慢變成非選中狀態(tài)。
// 加上此句擦秽,返回時(shí)直接就是非選中狀態(tài)码荔。
當(dāng)未使用 ARC 時(shí),你的 dealloc 實(shí)現(xiàn)必須把調(diào)用父類(lèi)的實(shí)現(xiàn)作為最后一條指令感挥。(隱含的意思就是缩搅,使用 ARC 時(shí)不能調(diào)用父類(lèi)的實(shí)現(xiàn));
但有些時(shí)候會(huì)發(fā)現(xiàn)控制器出棧的時(shí)候不會(huì)調(diào)用dealloc方法触幼,歸根結(jié)底硼瓣,是因?yàn)楫?dāng)前控制器被某個(gè)對(duì)象強(qiáng)引用并“握住”了,控制器的引用計(jì)數(shù)不為0置谦,系統(tǒng)無(wú)法幫你釋放這部分內(nèi)存堂鲤。
控制器被強(qiáng)引用的原因:
1.block塊使用不當(dāng)。因?yàn)閎lock會(huì)對(duì)方法中的變量自動(dòng)retain一次霉祸。請(qǐng)檢查控制器中block代碼筑累。
2.NSTimer沒(méi)有銷(xiāo)毀。在viewWillDisappear之前需要把控制器用到的NSTimer銷(xiāo)毀丝蹭,具體操作
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:nil userInfo:nil repeats:YES];
[timer invalidate]; // 銷(xiāo)毀timer
timer = nil; // 置nil
3.控制器中的代理屬性未設(shè)置成assign或weak慢宗。
removeFromSuperview會(huì)破壞cellForRowAtIndexPath的重用機(jī)制,若不是自定義的cell奔穿,就需要利用tag值給cell的創(chuàng)建subviews或換值來(lái)利用重用機(jī)制镜沽;若是自定義的cell就在cell的init方法里 addSubviews。
24.問(wèn)題1:當(dāng)tableview style設(shè)置為plain時(shí)贱田,每個(gè)section的header會(huì)懸浮在屏幕最上面缅茉。讓header也跟隨tableview一起上下滾動(dòng),不停在屏幕最上的部分:
//去掉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);
}
}
}
問(wèn)題2:當(dāng)tableview style設(shè)置為ground時(shí)男摧,每個(gè)section的header會(huì)跟隨tableview一起上下滾動(dòng)蔬墩,有時(shí)會(huì)發(fā)現(xiàn)footer或header總會(huì)有間距,這時(shí)就要設(shè)置:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.01;//不能為0耗拓;不能只重寫(xiě)任何一個(gè)代理方法拇颅,必須兩個(gè)都設(shè)置為很小的數(shù)。
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.01;
}
25.CoreGraphic框架中定義好的宏乔询,可判斷
CGPointEqualToPoint
CGSizeEqualToSize(size, size) == YES
在.m中( )中有名字就是類(lèi)別樟插,沒(méi)有就是Extention(匿名類(lèi)別)
@interface ViewController ( )
{
NSTimer *_timer;
}
@end
27.LaunchScreen.storyboard上只能放一些靜態(tài)圖片,并且不能關(guān)聯(lián)自定義的ViewController;
如果要?jiǎng)討B(tài)設(shè)置啟動(dòng)圖黄锤,或者啟動(dòng)動(dòng)畫(huà)搪缨,需要在APPDelegate中的Finish方法中,創(chuàng)建View鸵熟,把圖片加載到View上:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *){}
28.UITableViewController默認(rèn)的會(huì)在viewWillAppear的時(shí)候副编,清空所有選中cell。
設(shè)置self.clearsSelectionOnViewWillAppear = NO旅赢,來(lái)禁用該功能齿桃,
在viewDidAppear中調(diào)用UIScrollView的flashScrollIndicators方法讓滾動(dòng)條閃動(dòng)一次,提示用戶(hù)該控件是可以滑動(dòng)的煮盼。
原來(lái)當(dāng)程序第一次調(diào)用self.view的時(shí)候短纵,viewDidLoad方法就會(huì)被執(zhí)行,而不一定非要等到init之后willAppear之前僵控。這給我們敲響了警鐘香到,這樣的代碼就隱藏了問(wèn)題:
- (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;
}
這段代碼執(zhí)行完后的aInstanceVariable_是0而不是10086,可能會(huì)為一些bug深深地埋下一顆種子报破。
30.任務(wù)欄小菊花
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];