原網(wǎng)頁的地址:http://www.open-open.com/lib/view/open1463124922214.html
1弄诲,打印View所有子視圖
po [[self view]recursiveDescription]
2险领,layoutSubviews調(diào)用的調(diào)用時機
- 當視圖第一次顯示的時候會被調(diào)用
- 當這個視圖顯示到屏幕上了,點擊按鈕
- 添加子視圖也會調(diào)用這個方法
- 當本視圖的大小發(fā)生改變的時候是會調(diào)用的
- 當子視圖的frame發(fā)生改變的時候是會調(diào)用的
- 當刪除子視圖的時候是會調(diào)用的
3,NSString過濾特殊字符
// 定義一個特殊字符的集合
NSCharacterSet set = [NSCharacterSet characterSetWithCharactersInString:
@"@/:丽猬;()¥「」"爷抓、[]{}#%-+=\|~<>$€?'@#$%&*()+'""];
// 過濾字符串的特殊字符
NSString *newString = [trimString stringByTrimmingCharactersInSet:set];
4碘裕,TransForm屬性
//平移按鈕
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformTranslate(transForm, 10, 0);
//旋轉(zhuǎn)按鈕
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformRotate(transForm, M_PI_4);
//縮放按鈕
self.buttonView.transform = CGAffineTransformScale(transForm, 1.2, 1.2);
//初始化復位
self.buttonView.transform = CGAffineTransformIdentity;
5,去掉分割線多余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];
}
}
6,計算方法耗時時間間隔
// 獲取時間間隔
define TICK CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
define TOCK NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
7为迈,Color顏色宏定義
// 隨機顏色
define RANDOM_COLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
// 顏色(RGB)
define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
8丹壕,Alert提示宏定義
define Alert(S, ...) [[[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:(S), ##VA_ARGS] delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil] show]
8,讓 iOS 應(yīng)用直接退出
-
(void)exitApplication {
AppDelegate *app = [UIApplication sharedApplication].delegate;
UIWindow *window = app.window;[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
} completion:^(BOOL finished) {
exit(0);
}];
}
8沃疮,NSArray 快速求總和 最大值 最小值 和 平均值
NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);
9盒让,修改Label中不同文字顏色
(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self editStringColor:self.label.text editStr:@"好" color:[UIColor blueColor]];
}-
(void)editStringColor:(NSString *)string editStr:(NSString *)editStr color:(UIColor *)color {
// string為整體字符串, editStr為需要修改的字符串
NSRange range = [string rangeOfString:editStr];NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string];
// 設(shè)置屬性修改字體顏色UIColor與大小UIFont
[attribute addAttributes:@{NSForegroundColorAttributeName:color} range:range];self.label.attributedText = attribute;
}
10梅肤,播放聲音import<AVFoundation/AVFoundation.h>
// 1.獲取音效資源的路徑
NSString *path = [[NSBundle mainBundle]pathForResource:@"pour_milk" ofType:@"wav"];
// 2.將路勁轉(zhuǎn)化為url
NSURL *tempUrl = [NSURL fileURLWithPath:path];
// 3.用轉(zhuǎn)化成的url創(chuàng)建一個播放器
NSError *error = nil;
AVAudioPlayer *play = [[AVAudioPlayer alloc]initWithContentsOfURL:tempUrl error:&error];
self.player = play;
// 4.播放
[play play];
11,檢測是否IPad Pro -
(BOOL)isIpadPro
{
UIScreen *Screen = [UIScreen mainScreen];
CGFloat width = Screen.nativeBounds.size.width/Screen.nativeScale;
CGFloat height = Screen.nativeBounds.size.height/Screen.nativeScale;
BOOL isIpad =[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
BOOL hasIPadProWidth = fabs(width - 1024.f) < DBL_EPSILON;
BOOL hasIPadProHeight = fabs(height - 1366.f) < DBL_EPSILON;
return isIpad && hasIPadProHeight && hasIPadProWidth;
}
11邑茄,修改Tabbar Item的屬性// 修改標題位置
self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -10);
// 修改圖片位置
self.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0);// 批量修改屬性
for (UIBarItem *item in self.tabBarController.tabBar.items) {
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:19.0], NSFontAttributeName, nil]
forState:UIControlStateNormal];
}// 設(shè)置選中和未選中字體顏色
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];//未選中字體顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal];//選中字體顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor cyanColor]} forState:UIControlStateSelected];
12姨蝴,NULL - nil - Nil - NSNULL的區(qū)別
nil是OC的,空對象肺缕,地址指向空(0)的對象左医。對象的字面零值
Nil是Objective-C類的字面零值
NULL是C的,空地址同木,地址的數(shù)值是0浮梢,是個長整數(shù)
NSNull用于解決向NSArray和NSDictionary等集合中添加空值的問題
11,去掉BackBarButtonItem的文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
12彤路,控件不能交互的一些原因
1秕硝,控件的userInteractionEnabled = NO
2,透明度小于等于0.01洲尊,aplpha
3远豺,控件被隱藏的時候,hidden = YES
4坞嘀,子視圖的位置超出了父視圖的有效范圍躯护,子視圖無法交互,設(shè)置了
12姆吭,修改UITextField中Placeholder的文字顏色
[text setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
}
13榛做,視圖的生命周期
1、 alloc 創(chuàng)建對象内狸,分配空間
2检眯、 init (initWithNibName) 初始化對象,初始化數(shù)據(jù)
3昆淡、 loadView 從nib載入視圖 锰瘸,除非你沒有使用xib文件創(chuàng)建視圖
4、 viewDidLoad 載入完成昂灵,可以進行自定義數(shù)據(jù)以及動態(tài)創(chuàng)建其他控件
5避凝、 viewWillAppear視圖將出現(xiàn)在屏幕之前,馬上這個視圖就會被展現(xiàn)在屏幕上了
6眨补、 viewDidAppear 視圖已在屏幕上渲染完成
1管削、viewWillDisappear 視圖將被從屏幕上移除之前執(zhí)行
2、viewDidDisappear 視圖已經(jīng)被從屏幕上移除撑螺,用戶看不到這個視圖了
3含思、dealloc 視圖被銷毀,此處需要對你在init和viewDidLoad中創(chuàng)建的對象進行釋放.
viewVillUnload- 當內(nèi)存過低,即將釋放時調(diào)用含潘;
viewDidUnload-當內(nèi)存過低饲做,釋放一些不需要的視圖時調(diào)用。
14遏弱,應(yīng)用程序的生命周期
1盆均,啟動但還沒進入狀態(tài)保存 :
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2,基本完成程序準備開始運行:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
3漱逸,當應(yīng)用程序?qū)⒁敕腔顒訝顟B(tài)執(zhí)行泪姨,應(yīng)用程序不接收消息或事件,比如來電話了:
- (void)applicationWillResignActive:(UIApplication *)application
4虹脯,當應(yīng)用程序入活動狀態(tài)執(zhí)行驴娃,這個剛好跟上面那個方法相反:
- (void)applicationDidBecomeActive:(UIApplication *)application
5,當程序被推送到后臺的時候調(diào)用循集。所以要設(shè)置后臺繼續(xù)運行唇敞,則在這個函數(shù)里面設(shè)置即可:
- (void)applicationDidEnterBackground:(UIApplication *)application
6,當程序從后臺將要重新回到前臺時候調(diào)用咒彤,這個剛好跟上面的那個方法相反:
- (void)applicationWillEnterForeground:(UIApplication *)application
7疆柔,當程序?qū)⒁顺鍪潜徽{(diào)用,通常是用來保存數(shù)據(jù)和一些退出前的清理工作:
- (void)applicationWillTerminate:(UIApplication *)application
15镶柱,判斷view是不是指定視圖的子視圖
BOOL isView = [textView isDescendantOfView:self.view];
16旷档,判斷對象是否遵循了某協(xié)議
if ([self.selectedController conformsToProtocol:@protocol(RefreshPtotocol)]) {
[self.selectedController performSelector:@selector(onTriggerRefresh)];
}
17,頁面強制橫屏
pragma mark - 強制橫屏代碼
- (BOOL)shouldAutorotate{
//是否支持轉(zhuǎn)屏
return NO;
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
//支持哪些轉(zhuǎn)屏方向
return UIInterfaceOrientationMaskLandscape;
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight;
} - (BOOL)prefersStatusBarHidden{
return NO;
}
18歇拆,系統(tǒng)鍵盤通知消息
1鞋屈、UIKeyboardWillShowNotification-將要彈出鍵盤
2、UIKeyboardDidShowNotification-顯示鍵盤
3故觅、UIKeyboardWillHideNotification-將要隱藏鍵盤
4厂庇、UIKeyboardDidHideNotification-鍵盤已經(jīng)隱藏
5、UIKeyboardWillChangeFrameNotification-鍵盤將要改變frame
6输吏、UIKeyboardDidChangeFrameNotification-鍵盤已經(jīng)改變frame
19权旷,關(guān)閉navigationController的滑動返回手勢
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
20,設(shè)置狀態(tài)欄為任意的顏色
- (void)setStatusColor
{
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
statusBarView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:statusBarView];
}
21贯溅,讓Xcode的控制臺支持LLDB類型的打印
打開終端輸入三條命令:
touch ~/.lldbinit
echo display @import UIKit >> ~/.lldbinit
echo target stop-hook add -o "target stop-hook disable" >> ~/.lldbinit
下次重新運行項目拄氯,然后就不報錯了。
iOS 知識-常用小技巧大雜燴
22它浅,Label行間距
-(void)test{
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3];
//調(diào)整行間距
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [self.contentLabel.text length])];
self.contentLabel.attributedText = attributedString;
}
23译柏,UIImageView填充模式
@"UIViewContentModeScaleToFill", // 拉伸自適應(yīng)填滿整個視圖
@"UIViewContentModeScaleAspectFit", // 自適應(yīng)比例大小顯示
@"UIViewContentModeScaleAspectFill", // 原始大小顯示
@"UIViewContentModeRedraw", // 尺寸改變時重繪
@"UIViewContentModeCenter", // 中間
@"UIViewContentModeTop", // 頂部
@"UIViewContentModeBottom", // 底部
@"UIViewContentModeLeft", // 中間貼左
@"UIViewContentModeRight", // 中間貼右
@"UIViewContentModeTopLeft", // 貼左上
@"UIViewContentModeTopRight", // 貼右上
@"UIViewContentModeBottomLeft", // 貼左下
@"UIViewContentModeBottomRight", // 貼右下
24,宏定義檢測block是否可用
define BLOCK_EXEC(block, ...) if (block) { block(VA_ARGS); };
// 宏定義之前的用法
if (completionBlock) {
completionBlock(arg1, arg2);
}
// 宏定義之后的用法
BLOCK_EXEC(completionBlock, arg1, arg2);
文/品味_生活(簡書)
via:http://www.reibang.com/p/7c3ee5e67d03