UISearchBar 在 iOS 的不同版本中,把 subviews 列出來看會發現 view 層次結構不同,而要去除背景主要是將 UISearchBarBackground 給 remove 掉,因此就要針對各個 iOS 的版本做點調整,如下所示:
for (UIView *view in self.searchBar.subviews)
{
iOS 7 以前:
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[view removeFromSuperview];
break;
}
iOS 7 含以後:
if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0)
{
for (UIView *subView in view.subviews)
{
if ([subView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subView removeFromSuperview];
break;
}
}
}
}
2014年9月26日 星期五
2014年8月28日 星期四
[iOS] Switching from http to https. Invalid certificate
常用一些第三方套件提供網路通訊時,如果 server 端為 https 需要做一些設定來排除如標題所提到的問題,主要是設定允許 certificate 存取:
@interface NSURLRequest(Private)
+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;
@end
使用如下指令:
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[inURL host]];
如果是使用 AFNetworking 套件時再加入以下定義:(加在 .pch 檔內)
#ifdef DEBUG
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
#endif
參考來源:http://stackoverflow.com/questions/12447318/switching-from-http-to-https-invalid-certificate
@interface NSURLRequest(Private)
+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;
@end
使用如下指令:
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[inURL host]];
如果是使用 AFNetworking 套件時再加入以下定義:(加在 .pch 檔內)
#ifdef DEBUG
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
#endif
參考來源:http://stackoverflow.com/questions/12447318/switching-from-http-to-https-invalid-certificate
[iOS] NSTimer 機制小記
常常在用 NSTimer 來設計一些延遲或重覆執行某個 func call,但如果有用到 touch 事件,如:圖片點擊,UIScrollView 滑動等,就會把該 fund call 暫停,直到 touch 結束才繼續執行。
這樣一來就有時間差的問題存在,問了 google 大大後得知有人分享解決方式,就是將 NSTimer 加到別的 RunLoopMode 即可。
在 NSTimer 初始化後加入以下指令:
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
參考來源:http://stackoverflow.com/questions/6684016/why-timer-stops-when-scrolling-in-uiwebview-iphone
這樣一來就有時間差的問題存在,問了 google 大大後得知有人分享解決方式,就是將 NSTimer 加到別的 RunLoopMode 即可。
在 NSTimer 初始化後加入以下指令:
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
參考來源:http://stackoverflow.com/questions/6684016/why-timer-stops-when-scrolling-in-uiwebview-iphone
2014年8月19日 星期二
[iOS] 畫個虛線
- (void)makeLineInImageView:(UIImageView *)imageView
{
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //設定線條終點形狀
float lengths[] = {2, 2}; //第一個參數設定每個虛線寬度,第二個參數設定虛線的間隔
CGContextRef line = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
CGContextSetLineDash(line, 0, lengths, 2);
CGContextMoveToPoint(line, 0.0f, 0.0f); //開始畫線
CGContextAddLineToPoint(line, imageView.frame.size.width, 0.0f);
CGContextStrokePath(line);
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
}
{
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //設定線條終點形狀
float lengths[] = {2, 2}; //第一個參數設定每個虛線寬度,第二個參數設定虛線的間隔
CGContextRef line = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
CGContextSetLineDash(line, 0, lengths, 2);
CGContextMoveToPoint(line, 0.0f, 0.0f); //開始畫線
CGContextAddLineToPoint(line, imageView.frame.size.width, 0.0f);
CGContextStrokePath(line);
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
}
2014年7月30日 星期三
[iOS] How To Disable AFNetworking Cache
最近使用 AFNetworking 來下載圖片,發現在上傳圖後至 back-end 後,再更新會有 cache 的問題,在此註記一下如何忽略 cache 問題。主要是將 cachePolicy 設為 NSURLRequestReloadIgnoringLocalAndRemoteCacheData 即可。
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:20];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:20];
2014年7月19日 星期六
[iOS] Custom UIAlertView for iOS 7
自從 iOS 7 開始,很多人說無法加入 custom view。
若要帶輸入框(UITextField)的話,雖然本身有提供好幾種模式,如:UIAlertViewStyleLoginAndPasswordInput (帳號&密碼)、UIAlertViewStylePlainTextInput (文字)等。
結果發現要做個重設密碼,需要2個 UITextField 且為 Secure 模式就無法度了,因為 UIAlertViewStyleLoginAndPasswordInput 其中有1個不是 Secure 模式。
拜讀 google 大神後找到好心人提供方法,去設定 UIAlertView 裡的 accessoryView 參數即可。
[alertView setValue:customContentView forKey:@"accessoryView"];
參考來源:http://stackoverflow.com/questions/18886048/ios-multiple-text-fields-on-a-uialertview-in-ios-7
若要帶輸入框(UITextField)的話,雖然本身有提供好幾種模式,如:UIAlertViewStyleLoginAndPasswordInput (帳號&密碼)、UIAlertViewStylePlainTextInput (文字)等。
結果發現要做個重設密碼,需要2個 UITextField 且為 Secure 模式就無法度了,因為 UIAlertViewStyleLoginAndPasswordInput 其中有1個不是 Secure 模式。
拜讀 google 大神後找到好心人提供方法,去設定 UIAlertView 裡的 accessoryView 參數即可。
[alertView setValue:customContentView forKey:@"accessoryView"];
參考來源:http://stackoverflow.com/questions/18886048/ios-multiple-text-fields-on-a-uialertview-in-ios-7
訂閱:
文章 (Atom)