1、去掉UITableView沒有內容顯示的cell
self.tableView.tableFooterView = [UIView new];
2、修改UITableView自帶的線條(顏色和位置)
//ios8 設置UITableViewCell左側默認15像素的空白
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.tableView setSeparatorColor:SC_TABLEVIE_LINELIGTHT];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
3、約束動畫實現
self.shareBtnBgTopConstraint.constant = -102;
// 告訴約束需要更新
[self.shareBtnBgView setNeedsUpdateConstraints];
// 調用此方法告訴檢測是否需要更新約束,若需要則更新,下面添加動畫效果才起作用
[self.shareBtnBgView updateConstraintsIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
self.shareView.alpha = 0.6;
[self.view layoutIfNeeded];
}];
4、單例實現方法
+ (HttpRequestEngine *)shareInstance{
static HttpRequestEngine *httpEngine = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
httpEngine = [[HttpRequestEngine alloc] init];
});
return httpEngine;
}
5、AFNetWorking監聽網絡變化
#pragma mark - 監聽網絡變化
- (void)buggedNetWorkChange{
AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
[afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:{
NSLog(@"無網絡");
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:{
NSLog(@"WiFi網絡");
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:{
NSLog(@"2G/3G/4G網絡");
break;
}
default:
break;
}
}];
[afNetworkReachabilityManager startMonitoring];
}
【6】UITextField占位文字的顏色的修改(.h和.m文件實現)
@interface UITextField (PHColor)
/**
* 占位文字顏色
*/
@property (strong, nonatomic) UIColor *placeholderColor;
@end
@implementation UITextField (PHColor)
- (void)setPlaceholderColor:(UIColor *)placeholderColor{
BOOL change = NO;
if (self.placeholder == nil) {
self.placeholder = @" ";
change = YES;
}
[self setValue:placeholderColor forKeyPath:@"placeholderLabel.textColor"];
if (change) {
self.placeholderColor = nil;
}
}
- (UIColor *)placeholderColor{
return [self valueForKey:@"placeholderLabel.textColor"];
}
@end
【7】iOS導航欄NavigationBar設置透明,以及添加變色的動畫
透明只需設置NavigationBar的背景圖片為一張空圖片即可
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
NavigationBar下邊有一個ShadowImage,也可以通過設置空的UIImage設置透明
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
變色動畫,在NavigationBar下插入一個view,執行動畫改變這個view的透明度即可。
CGRect frame = self.navigationController.navigationBar.frame;
_alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, frame.size.width, frame.size.height+20)];
_alphaView.backgroundColor = [UIColor greenColor];
_alphaView.userInteractionEnabled = NO;
[self.navigationController.navigationBar insertSubview:_alphaView atIndex:0];
【8】iOS 實現ScrollView 上滑隱藏Navigationbar,下滑顯示
#pragma mark 滑動隱藏導航欄
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if (velocity.y>0) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
【9】去掉UIWebView中彈框的alertView的title
Js中有Alert、confirm和promopt三種類似iOS本地的UIAlertView 樣子,但是在iOS的app中彈出alertView的title是url地址,不怎么美觀,可以用以下方法解決:
///新建一個UIWebView的擴展類,然后在用到webView的類里引用頭文件就OK了
@interface UIWebView (JavaScripAlert) <UIAlertViewDelegate>
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;
- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;
@end
@implementation UIWebView (JavaScripAlert)
static BOOL status = NO;
static BOOL isEnd =NO;
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {
UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"提示"
message:message
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[customAlert show];
}
- (NSString *) webView:(UIWebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(id)frame {
return @"";
}
- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {
UIAlertView *confirmDiag = [[UIAlertView alloc] initWithTitle:@"提示"
message:message
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"確定",nil];
[confirmDiag show];
CGFloat version = [[[UIDevice currentDevice] systemVersion]floatValue];
if (version >= 7.) {
while (isEnd == NO) {
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
}
}else {
while (isEnd ==NO && confirmDiag.superview !=nil) {
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
}
}
isEnd = NO;
return status;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
status = buttonIndex;
isEnd = YES;
}
@end
【10】獲取磁盤總空間大小
+ (CGFloat)diskOfAllSizeMBytes {
CGFloat size = 0.0;
NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"error: %@", error.localizedDescription);
#endif
}else{
NSNumber *number = [dic objectForKey:NSFileSystemSize];
size = [number floatValue]/1024/1024;
}
return size;
}
獲取磁盤可用空間大小
+ (CGFloat)diskOfFreeSizeMBytes{
CGFloat size = 0.0;
NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@"error: %@", error.localizedDescription);
#endif
}else{
NSNumber *number = [dic objectForKey:NSFileSystemFreeSize];
size = [number floatValue]/1024/1024;
}
return size;
}
【11】判斷字符串中是否含有中文
+ (BOOL)isHaveChineseInString:(NSString *)string {
for(NSInteger i = 0; i < [string length]; i++){
int a = [string characterAtIndex:i];
if (a >= 0x4e00 && a < 0x9fff) {
return YES;
}
}
return NO;
}
【12】Xcode調試:
a、快速定位約束沖突方法:
添加UIViewAlertForUnsatisfiableConstraints的斷點
b、常用調試斷點
[NSException raise]
objc_exception_throw
【13】當遇到一些老古董設備的時候,程序運行出現以下錯誤:
dyld: Library not loaded: /System/Library/Frameworks/Contacts.framework/Contacts
Referenced from: /private/var/mobile/Containers/Bundle/Application/90A475A5-8EC4-416A-A354-0401D1CF5152/Butler.app/Butler
Reason: image not found
解決辦法:
Project-> Targets-> Build Phases-> Link Binary with Libraries
設置 Contacts.framework 的 status 為 Optional 搞定!
【14】UITableViewCell處于選中狀態下,UIView的背景顏色消失的解決辦法:
/// 在cell的以下兩個方法里重新設置view的背景顏色
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.pointView.backgroundColor = SC_TEXT_GREEN_COLOR;
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
self.pointView.backgroundColor = SC_TEXT_GREEN_COLOR;
}
待續,持續完善...