iPhone较为基础的代码片段
Iphone代码片段导航
1.给UITableViewController添加ToolBar。
self.navigationController.toolbarHidden = NO; //默认是隐藏的。
//添加MessageToolBar ,messageToolBar是IBOutlet的一个ToolBar。
self.toolbarItems = [[[NSMutableArray alloc] initWithArray:self.messageToolBar.items] autorelease];
self.navigationController.toolbar.barStyle = self.messageToolBar.barStyle;
2.后台运行一个方法,如果该方法需要修改UI,为了防止出错,应在主线程里修改UI。
[self performSelectorInBackground:@selector(updateInfo)];
在UpdateInfo里如果要修改UI ,
[self performSelectorOnMainThread:@selector(updateUIMethod) withObject:nil waitUntilDone:NO];
同时注意,后台程序的方法应该放在NSAutoRelease pool里的,如下所示:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
xxxx
[pool release];
3.在A类里动态的设定B类或者C类的方法。
[self.actionTarget performSelector:self.actionMethod withObject:parameter];
actionTarget -> id类型的属性。设置B 类或者C类。
actionMethod -> Sel类型的属性。设置具体的方法名
parameter -> 参数
4.设置Navigation的提示信息和进度条设置
self.navigationItem.prompt : 提示信息
   self.navigationItem.titleView :存放ProgressBar等其它提示信息的View
在进度条显示完了后,需要清空显示进度信息:
self.navigationItem.prompt = nil;
   self.navigationItem.titleView = nil;
5.从资源文件xib里加载View的方法
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyView"
                                                         owner:self
                                                       options:nil];
MyView *view = [nib objectAtIndex:0];
6. UIAlterView 修改默认的Frame高度
在其委托里实现这个方法
-(void)willPresentAlertView:(UIAlertView*)alertView 
{
    alertView.frame =CGRectMake(5.f,1.f,100.f,200.f);
}
参考:http://stackoverflow.com/questions/2763713/change-width-of-uialertview-in-ipad
 7.获取iphone屏幕大小
CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];
CGRect screenRect= [ [ UIScreen mainScreen ] applicationframe ];
8. 修改TableView的样式,让UITableView显示Windows的背景图片。
self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = nil;
如果要修改UITableCell的事情backgroundColor需要再 tableView:willDisplayCell:forRowAtIndexPath:里修改。
9.通过图片获取颜色。
修改分割线颜色
self.tableView.separatorColor = [UIColor blackColor];
显示文本的地方设置透明色
cell.textLabel.opaque = NO;
这样整个cell就有立体感。
10.设置UITableView 的checkmark显示样式
修改cell的 accessoryView
cell.accessoryView = UIImageView
11. 修改TableView距离导航缆的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
12. 自定义TableViewCell的背景颜色和选择后的颜色。
方法一:将TableViewCell的backgroundView和SelectBackGroundView修改成指定的View就可以了。
方法二: 在Interface Builder里设置cell的image和SelectImage属性,但是要记得UItableView修改seperator的属性为None
13 颜色定义。
美工一般定义好颜色,然后让程序员去填充颜色,美工一般给的是RGB颜色,那么RGB颜色如果换成UIColor
[UIColor colorWithRed:31.0/255 green:204.0/255 blue:39.0/255 alpha:1.0];
Red,Green,Blue只接受0-1的参数,换算方法是除以255。
14. Xcode 4设置 NSZombieEnabled
if you click on the scheme drop down bar -> edit scheme -> arguments tab and then add NSZombieEnabled in the Environment Variables column and YES in the value column
15.自动生成多语言化的StringTable
如果在代码里全部是通过 NSLocalizedString(@"中文", nil)来对应多语言,最后要整理一个list,手动一个一个粘贴太麻烦。
自动化生成方法:在命令行目录下进入项目根目录:执行 genstrings -a $(find . -name "*.m"),就会自动生成一个文件对应。
参考网址 http://steelwheels.sourceforge.jp/Documents/genstring.html
http://iphone.longearth.net/2009/05/25/%E3%80%90iphone%E3%80%91localizablestrings%E3%82%92%E8%87%AA%E5%8B%95%E3%81%A7%E4%BD%9C%E3%82%8B-genstrings/
16.自定义bond字体
[UIFont fontWithName:@"Helvetica-Bold" size:16.0]
17 无边框透明UITableViewCell
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
--Cell修改--
self.backgroundView = [[[UIView alloc] init] autorelease];
self.backgroundView.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = [[[UIView alloc] init] autorelease];
self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
18. 隐藏Tabbar
SampleViewController*obj =[[SampleViewController alloc] init];
[obj setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:obj animated:YES];
[obj release];19.从UIView获取UImage- (UIImage *)getImageFromView:(UIView *)orgView
{ UIGraphicsBeginImageContext(orgView.bounds.size);
[orgView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
20. 添加手式识别后,会屏蔽掉touchend方法
21.获取手机号码,和IMEI
[[NSUserDefaults standardUserDefaults] valueForKey:@"SBFormattedPhoneNumber"];
获取手机的imei
#import "Message/NetworkController.h"
NetworkController *ntc=[[NetworkController sharedInstance] autorelease];
NSString *imeistring = [ntc IMEI];
imeistring就是获取的imei。 IMEI(International Mobile Equipment Identity)是国际移动设备身份码的缩写,国际移动装备辨识码,是由15位数字组成的"电子串号",它与每台手机一一对应,而且该码是全世界唯一的。
22 NLog的格式,经常忘记,做个笔记
23.更改UISearchBar最下面黑色的边框
#define SEARCHBAR_BORDER_TAG 1337
24.设置键盘的默认形式。
比如UITextField 设置为默认数字,和只允许数组数字
//默认数字
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation
   //只允许输入数字
textField.keyboardType = UIKeyboardTypeNumberPad
25.UIButton设置文字左对齐
[myLabel release];
26. retain异常的时候重载这个方法设置断点查看和分析
- (id) retain
27.去掉白色半圆
Plist添加
Icon already includes gloss effects 为YES
UIPrerenderedIcon 设置不起作用(Xcode4 .0.2)
28.tableView reloadRowsAtIndexPaths 如果不在可见区域,将不会重新加载。
29. 设置应用程序的statusbaryanse
再plist里设置Status bar style Opaque black style
30. 设置控件的copy paste的本地化
- 设置Localization native development region =》 china
- 将项目的en.lproj 改成zh_CN.lproj
31. 允许应用程序通过itunes上传文件(ios3.2以上)
在info.plist里设置 UIFileSharingEnabled => YES
32. 获取UICOLOR的rgb值
NSLog(@"Alpha: %f", CGColorGetAlpha(SelectedColor.CGColor));
33.获取2个时间之间的天,小时,分钟
+(NSString *)TimeRemainingUntilDate:(NSDate *)date {
34. Icon specified in the Info.plist not found under the top level app wrapper
记住Icon 首字母是大写的,不是icon.png , 是Icon.png
35.
http://www.cnblogs.com/baryon/archive/2010/05/06/1728968.html
http://www.douban.com/note/131009422/
36.获取手机唯一ID
NSString *deviceUID = [[NSString alloc] initWithString:[device uniqueIdentifier]];
NSLog(@"%@",deviceUID); // 输出设备id
37 .动态调用一个类的方法
38.改变NavigationViewController默认动画,让其旋转
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
39 .显示和隐藏StatsBar
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
启动隐藏StatusBar info.plist 添加 Status bar is initially hidden 为bool Yes
40 . 点击某个cell的按钮,收藏到tabbar里
http://stackoverflow.com/questions/5926554/get-uitableviewcell-position-from-visible-area-or-window
41. CoreText用文字填充不规则图形
CGContextRef context = UIGraphicsGetCurrentContext();
// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0); // Create a path to render text in
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds );
// An attributed string containing the text to render
NSAttributedString* attString = [[NSAttributedString alloc]
initWithString:...];
// Create a path to wrap around
CGMutablePathRef clipPath = CGPathCreateMutable();
CGPathAddEllipseInRect(clipPath, NULL, CGRectMake(200, 200, 300, 300) ); // A CFDictionary containing the clipping path
CFStringRef keys[] = { kCTFramePathClippingPathAttributeName };
CFTypeRef values[] = { clipPath };
CFDictionaryRef clippingPathDict = CFDictionaryCreate(NULL,
(const void **)&keys, (const void **)&values,
sizeof(keys) / sizeof(keys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks); // An array of clipping paths -- you can use more than one if needed!
NSArray *clippingPaths = [NSArray arrayWithObject:(NSDictionary*)clippingPathDict];
// Create an options dictionary, to pass in to CTFramesetter
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:clippingPaths forKey:(NSString*)kCTFrameClippingPathsAttributeName]; // Finally create the framesetter and render text
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); //3
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, [attString length]), path, optionsDict);
CTFrameDraw(frame, context);
// Clean up
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);
http://amyworrall.com/post/11098565269/text-wrap-with-core-text
42 . Animation开始和结束callback
UIView
- (void)animateStuff {
[UIView beginAnimations:@"animationName" context:nil];
[UIView setAnimationDelegate:self];
[self.view doWhatever];
[UIView commitAnimations];
} - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([finished boolValue]) {
NSLog(@"Animation Done!");
}
CoreAnimation
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"]; anim.delegate = self;
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
}
iPhone较为基础的代码片段的更多相关文章
- 直接拿来用 九个超实用的PHP代码片段(二)
		每位程序员和开发者都喜欢讨论他们最爱的代码片段,尤其是当PHP开发者花费数个小时为网页编码或创建应用时,他们更知道这些代码的重要性.为了节约编码时间,笔者收集了一些较为实用的代码片段,帮助开发者提高工 ... 
- 10个PHP代码片段
		还记得CSDN研发频道此前发表过的一篇<可以直接拿来用的15个jQuery代码片段>吗?本文笔者将继续为你奉上10个超级有用的PHP代码片段. PHP是一种HTML内嵌式的语言,是一种在服 ... 
- VS里的 代码片段(Code snippet)很有用,制作也很简单
		工欲善其事必先利其器,而 Visual Studio 就是我们的开发利器. 上一篇文章,介绍了一个很棒的快捷键,如果你还没用过这个快捷键,看完之后应该会豁然开朗.如果你已经熟练的应用它,也会温故而知新 ... 
- VSCode--HTML代码片段(基础版,react、vue、jquery)
		起因是最近在学习前端,看的网上的demo也是在react.vue.jquery之间穿插,为了方便一键生成html模板(懒)写demo,有了以下折腾. 本人使用的前端编辑工具是vscode(方便.懒), ... 
- 拥有的50个CSS代码片段(上)
		1. CSS 重置 ;;;font-size: 100%; font: inherit; vertical-align: baseline; outline: none; -webkit-box-si ... 
- CSS的50个代码片段
		1.css全局 html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a ... 
- (转)每位设计师都应该拥有的50个CSS代码片段
		原文地址:http://www.cnblogs.com/fengyuqing/archive/2013/06/15/css_50.html 面对每年如此多的 新趋势 ,保持行业的领先是个很困难问题. ... 
- 很实用的50个CSS代码片段
		原文:50 Useful CSS Snippets Every Designer Should Have 面对每年如此多的 新趋势 ,保持行业的率先是个非常困难问题. 站点设计者和前 ... 
- js/jquery/html前端开发常用到代码片段
		1.IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法.条件注释只能用于IE5以上,IE ... 
随机推荐
- JS学习笔记(6)--音乐播放器
			说明(2017.3.15): 1. lrc.js里面存储LRC歌词的格式的数组,获取里面的时间轴,转为秒数. 2. 通过audio.currentTime属性,setinterval每秒获取歌曲播放的 ... 
- JavaScript 使用 php 的变量
			php 里面有一个变量,我想让 js 调用他, 有如下流程: <?php for ($i = 0; $i < 8; $i++) { echo "<tr>"; ... 
- /usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory
			cc -DDEBUG -mtune=core2 -O2 \ -onvideo nvideo.c \ -I/usr/include/atk-1.0 \ -I/usr/include/cairo \ -I ... 
- docker 部署nginx+weblogic集群
			測试一段时间.再来报告 
- elasticsearch安装与使用(6)-- Logstash安装与配置
			由于elasticsearch的search guard 不支持 elasticsearch的jdbc,所以如果安装了search guard认证插件之后,基本上jdbc就废了,所以我们需要用到log ... 
- HTML5之IndexedDB使用详解
			随着firefox4正式版的推出,IndexedDB正式进入我们的视线.IndexedDB是HTML5-WebStorage的重要一环,是一种轻量级NOSQL数据库.相较之下,WebDataBase标 ... 
- 10 个强大的JavaScript / jQuery 模板引擎推荐
			模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档.由于在开发过程中,网站或应用程序的界面与数据实现分离,大大提升了开发效率,良好的设计也使得代码重用变得更加容易. 本文 ... 
- Python 之 向上取整、向下取整以及四舍五入函数
			import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ... 
- R语言低级绘图函数-rect
			rect 函数用来在一张图上添加矩形,只需要指定左下角和右上角的坐标的位置,就可以画出一个矩形 基本用法: plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), ... 
- 【Java  NIO的深入研究6】JAVA NIO之Scatter/Gather
			Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作. 分散(s ... 
