IOS 開發中小功能積累


1、設置tableview返回時取消選中狀態

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [self.tableview deselectRowAtIndexPath:self.tableview.indexPathForSelectedRow animated:YES];

 

}

 

2、設置UIPickerView默認選中

 

[pickerView selectRow:5 inComponent:0 animated:NO];

 

 

3、設置應用電池欄顏色

 

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

 

4、檢測網絡連接狀態 (添加SystemConfiguration.framework 和Reachability.h 和Reachability.m)

 

 

   if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) && 

        ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {

        

      NSLog(@"無網絡連接");  

    }

 

5、圖片縮放

 

UIImage *image = [[UIImage allocinitWithData:self.activeDownload];

 

 if (image.size.width != kAppIconHeight && image.size.height != kAppIconHeight)

{

 

CGSize itemSize = CGSizeMake(kAppIconHeightkAppIconHeight);

UIGraphicsBeginImageContext(itemSize);

CGRect imageRect = CGRectMake(0.00.0, itemSize.width, itemSize.height);

[image drawInRect:imageRect];

self.appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

 

 }

 else

 {

        self.appRecord.appIcon = image;

 }

 

 

6、設置UIWenview背景透明的方法

 

webview.backgroundColor = [UIColor clearColor];

webview.opaque = NO;

在 網頁里設置:

<body style="background-color: transparent">

 

 

7、js控制UIWebvie

js里:

 

<script>  

    function jump()  

    {  

        var clicked=true;   

        window.location="/clicked";     //改變URL  注意:要使用"/"分隔符  

        alert("js alert :jump");  

    }  

</script>  

 

 

oc里:

 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType  

{  

    //獲取URL並且做比較,判斷是否觸發了JS事件,注意有"/"  

    if ([request.mainDocumentURL.relativePath isEqualToString:@"/clicked"]) {  

        UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"you click me" message:@"clicked" delegate:nilcancelButtonTitle:@"sure" otherButtonTitles:@"cancel"nil];  

        [alertView show];  

        [alertView release];  

 

        return false;  

    }  

    return  true;  

}  

 

 

 

8、UIWebview加載本地html

 

 

  1. NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  
  2. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
 
9、設置UIwebview 縮放系數

UIWebView類沒有修改縮放系數的方法,我們只能用HTML代碼來做。Meta標簽可以設置viewport,而viewport就包含了初始化縮放系數的參數。

META標簽如下所示:

<meta name="viewport"content="minimum-scale=0.6; maximum-scale=5;  initial-scale=1; user-scalable=yes; width=640">

可以使用的參數有:

  • minimum-scale:
    允許縮放的最小倍數。默認為0.25,允許值為0-10。
  • maximum-scale:
    運行縮放的最大倍數。默認1.6,允許值為0-10。
  • initial-scale:
    當web頁被加載,還未被用戶縮放之前默認的縮放系數。默認值是自動根據頁面大小和可用區域計算出來的,但這個值最終會在最小倍數到最大倍數之間。
  • user-scalable
    是否運行用戶縮放該web頁。
  • width:
    viewpoint的寬。默認為980像素(iPhone)。允許值為200-10000 。”device-width”表示設備寬度(iPhone為320,iPad為768)。注意device width不等於用戶界面的寬度。設備寬度總是設備處於人像模式下的寬度(屏幕方向為正向)。如果我們想增加web頁的最大縮放系數(默認1.6),我們只需要在HTML代碼中增加META標簽,指定maximum-scale屬性即可。你可以直接在HTML源代碼中加入META標簽。如果web頁來自internet並且無法修改HTML源代碼,你可以用Javascript代碼創建META標簽並附加到web頁的HTML代碼中。讀完剩下的內容,你就知道怎么做了。
  • height:
    viewport的高。通常是根據width計算的。
 
10、點擊home鍵觸發AppDelegate.m中的

- (void)applicationWillResignActive:(UIApplication *)application

 

 

11、NSData 與NSString 互轉

 

 

NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];

NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding]; 

 

12、圖片轉圓角

 

 

效果圖

首先導入頭文件:#import <QuartzCore/QuartzCore.h>

UIImageView * headerImage = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, 64.0, 64.0)];

headerImage.image = contactPhoto;

CALayer * layer = [headerImage layer];

[layer setMasksToBounds:YES];

[layer setCornerRadius:10.0];

[layer setBorderWidth:1.0];

[layer setBorderColor:[[UIColor blackColor] CGColor]];

[contactHeader addSubview:headerImage];

 

13、將圖片保存到相冊的代碼

 

UIImageWriteToSavedPhotosAlbum(Uiimagenilnilnil);

 

14、去掉tableview 的線

 

[self.tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];

 

15、UILable文字加陰影

 

 titleText.shadowColor = [UIColor blackColor];

 titleText.shadowOffset = CGSizeMake(0, -1.0);

 

16、plist文件轉NSDictionary

 

 

     NSBundle *bundle = [NSBundle mainBundle];   

 

    //取得文件路徑  

    NSString *plistPath = [bundle pathForResource:@"cityData" ofType:@"plist"];   

 

    //讀取到一個NSDictionary  

    cityDictionary = [[NSDictionary allocinitWithContentsOfFile:plistPath];

 

 

17、隱藏tabbar

 

- (void) hideTabBar:(BOOL) hidden {

 

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0];

 

    for(UIView *view in self.tabBarController.view.subviews)

    {

        if([view isKindOfClass:[UITabBar class]])

        {

            if (hidden) {

                [view setFrame:CGRectMake(view.frame.origin.x480, view.frame.size.width, view.frame.size.height)];

            } else {

                [view setFrame:CGRectMake(view.frame.origin.x480-49, view.frame.size.width, view.frame.size.height)];

            }

        } 

        else 

        {

            if (hidden) {

                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width480)];

            } else {

                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width480-49)];

            }

        }

    }

 

    [UIView commitAnimations];

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM