首页 > 代码库 > 小经验
小经验
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 alloc] initWithData:self.activeDownload];
if (image.size.width != kAppIconHeight && image.size.height != kAppIconHeight)
{
CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.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="
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 alloc] initWithTitle:@"you click me" message:@"clicked" delegate:nilcancelButtonTitle:@"sure" otherButtonTitles:@"cancel", nil];
[alertView show];
[alertView release];
return false;
}
return true;
}
8、UIWebview加载本地html
- NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
- [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
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计算的。
- (void)applicationWillResignActive:(UIApplication *)application
11、NSData 与NSString 互转
NSData* xmlData = http://www.mamicode.com/[@"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(Uiimage, nil, nil, nil);
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 alloc] initWithContentsOfFile: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.x, 480, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, 480-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.width, 480)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480-49)];
}
}
}
[UIView commitAnimations];
}