1、获取屏幕尺寸
[UIScreen mainScreen].bounds
2、在webView中加载本地文档
-(void)loadDocument:(NSString*)documentPathStr inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentPathStr ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
3、改变searchBar取消按钮的样式
-(void)changeCancelBtnImage:(UISearchBar *)searchBar
{
for (id btn in searchBar.subviews) {
if ([btn isKindOfClass:[UIButton class]]) {
UIButton *tempBtn=(UIButton *)btn;
[tempBtn setHighlighted:YES];
[tempBtn setBackgroundImage:[UIImage imageNamed:@"btn_yh_bt.png"] forState:UIControlStateNormal];
[tempBtn setBackgroundImage:[UIImage imageNamed:@"btn_yh_bt.png"] forState:UIControlStateHighlighted];
tempBtn.showsTouchWhenHighlighted=YES;
}
}
}
4、取消按钮上的手势,去除指定控件上的手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]]) {
return NO;
}
else {
return YES;
}
}
5、退出下一页面试隐藏tabBar
losisticsView.hidesBottomBarWhenPushed=YES;
6、取消tableViewCell选中状态
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//some functions
......
// 取消选中状态
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
7、进行网络请求时,地址前必须加上http://字头,不然会造成联网失败!!
8、将字典转换为json数据
#import "SBJsonWriter.h"
...
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *jsonString = [jsonWriter stringWithObject:myDictionary];
[jsonWriter release];
[addressTableView setContentOffset:CGPointMake(0, 140+(textField.tag-5)*50) animated:YES];
11、在对tableViewCell里的控件进行初始化时,除了if还有else(苹果文档有详细介绍)
12、为navigationBar添加背景图片
ios4以前,在.m文件中添加如下类别
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"title_bg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
//ios5 later 在viewWillAppear
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Switch@2x.png"] forBarMetrics:UIBarMetricsDefault];
}
13、访问设备数据,设备版本号,应用程序版本信息
UIDevice *currentDevice = [UIDevice currentDevice];//获取设备信息 NSString *model = [currentDevice model];//当前设备名称 NSString *systemVersion = [currentDevice systemVersion];//当前设备版本号 NSArray *languageArray = [NSLocale preferredLanguages]; NSString *language = [languageArray objectAtIndex:0];//用户语言 NSLocale *locale = [NSLocale currentLocale]; NSString *country = [locale localeIdentifier];//用户所在地信息
应用程序的名称和版本号等信息都保存在mainBundle的一个字典中,用下面代码可以取出来。
NSString *deviceSpecs = [NSString stringWithFormat:@"%@ - %@ - %@ - %@ - %@", model, systemVersion, language, country, appVersion]; NSLog(@"Device Specs --> %@",deviceSpecs);NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary]; NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"]; NSString*appName =[infoDict objectForKey:@"CFBundleDisplayName"];
14、用浏览器打开链接
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
15、程序进入后台出发的方法
按home键切换到后台
- (void)applicationDidEnterBackground:(UIApplication *)application
按home键切回应用
- (void)applicationWillEnterForeground:(UIApplication *)application
16、如果想使subView屏蔽上层的点击事件就使superView的userInteractionEnabled=YES,也就是只有在父View可以交互时才会阻断子view的交互。
17、将图片(imageView)加载到window窗口上
[[UIApplication sharedApplication].keyWindow addSubview:imageView];
18、退出ios程序(不推荐主动退出程序,可以只给用户一个选项)
- (void)applicationWillTerminate:(UIApplication *)application
{
exit(0);
}
19、__NSCFBoolean类型
[NSNumber numberWithBool:YES]消息会得到Number的子类NSCFBoolean的一个实例
20、Drawing的一些属性
Drawing属性:
第一个复选:Opaque:选中它将通知iPhoneOS,视图下的任何内容都不应绘制,并且允许IPhone的绘图方法通过一些优化来加速绘图。为什么要选中Opaque复选框呢?我们举一个例子:当Alpha的值设定为1.0(不透明),Alpha值适用于将被绘制的图像部分,但是如果某个图像为完全填充图像视图,或者图像上存在一些洞,则下方的对象将可见,而与Alpha的值无关。选中Opaque复选框后,iPhone就会知道视图下方的任何内容都不需要绘制出来。
Hidden复选框:选中它后,用户不能看到此控件。 有时隐藏空间是非常有用的,比如通过开关来控制选项的隐藏和可见性。
Clip Subviews:如果你的视图有子视图,那么这些子视图并不完全包含在其父视图中,则此复选框将确定子视图的绘制方式。如果选中了Clip Subviews,只有在父视图范围内的子视图部分被绘制出来。如果未选中Clip Subviews,则全部子视图都将绘制出来,而不管它是否在父视图内部。(默认开启)
Clear Context Before Drawing:选中后,iPhone将使用透明黑色绘制空间覆盖的所有区域,然后才实际绘制空间。因为考虑到性能问题,所以使用情况很少,默认为关闭状态。
Interaction中有两个复选框:
User Interaction Enabled:指定用户能否对此对象进行操作。对于大多数空间,此复选框都是选中的。如果不选就不能出发操作方法。但标签和图像视图默认未选中,因为经常仅用于显示静态信息
Multiple Touch:用于确保此控件能否接受多点触摸事件。支持多种复杂的手势。
21、关于屏幕像素问题
ios中的控件的坐标是以一个像素为基本单位的,如果坐标精确度小于一个像素(如0.5个像素)会导致控件重影,所以坐标要去正roundf()
22、修改UISerachBar放大镜图片
for(id cc in [_searchBar subviews])
{
if([cc isKindOfClass:[UITextField class]])
{
UITextField *textField = (UITextField *)cc;
UIImage *image = [UIImage imageNamed:@"searchText.png"] ;
UIImageView *view = [[UIImageView alloc] initWithImage:image];
textField.leftView = view;
}
}
23、convertRect:toView:和convertRect:fromView:的使用
使用 convertRect: fromView: 或者 convertRect: toView:
例如一个视图控制器的view中有一个UITableView,UITableView的某个cell中有个UITextField,想要得到UITextField在view中的位置,就要靠上面的两个方法了。
用
CGRect rect = [self.view convertRect:textField.frame fromView:textField.superview];
或者
CGRect rect = [textField.superview convertRect:textField.frame toView:self.view];
进行转换都可以
24、根据内容计算label的高度
CGSize theTextS=[theText sizeWithFont:theFont constrainedToSize:CGSizeMake(theW, CGFLOAT_MAX) lineBreakMode:UILineBreakModeCharacterWrap];
sizeWithFont参数要和label上的参数类型相同否则会导致计算高度的不准确
typedef enum {
UILineBreakModeWordWrap = 0,
UILineBreakModeCharacterWrap,
UILineBreakModeClip,
UILineBreakModeHeadTruncation,
UILineBreakModeTailTruncation,
UILineBreakModeMiddleTruncation,
} UILineBreakMode;
UILineBreakModeWordWrap = 0,
以单词为单位换行,以单位为单位截断。
UILineBreakModeCharacterWrap,
以字符为单位换行,以字符为单位截断。
UILineBreakModeClip,
以单词为单位换行。以字符为单位截断。
UILineBreakModeHeadTruncation,
以单词为单位换行。如果是单行,则开始部分有省略号。如果是多行,则中间有省略号,省略号后面有4个字符。
UILineBreakModeTailTruncation,
以单词为单位换行。无论是单行还是多行,都是末尾有省略号。
UILineBreakModeMiddleTruncation,
以单词为单位换行。无论是单行还是多行,都是中间有省略号,省略号后面只有2个字符。
25、对url进行编码和解码
对特殊字符进行编码 stringByAddingPercentEscapesUsingEncoding
对特殊字符进行解码 stringByReplacingPercentEscapesUsingEncoding
26、比较字符串
-(void)searchName:(NSString *)theSearchName inNameArray:(NSArray *)theStoreNameArray
{
for (NSString *theNameStr in theStoreNameArray) {
NSComparisonResult theResult=[theNameStr compare:theSearchName options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, theSearchName.length)];
if (theResult==NSOrderedSame) {
[self.theReslutArray addObject:theNameStr];
}
}
}
range的参数截取是theNameStr的长度即截取的是比较的对象
27、计算两个时间的时间差
-(NSString *)calculateTimeDifFromDate:(NSDate *)theFromDate toDate:(NSDate *)theToDate
{
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *d = [cal components:unitFlags fromDate:theFromDate toDate:theToDate options:0];
int sec = [d hour]*3600+[d minute]*60+[d second];
NSLog(@"sec-----%d",sec);
NSLog(@"second = %d",[d hour]*3600+[d minute]*60+[d second]);
int theYear=[d year];
int theMonth=[d month];
int theDay=[d day];
int theHours=[d hour];
int theMinute=[d minute];
NSString *theDifTimeStr=[NSString stringWithFormat:@"%d年%d月%d天%d小时%d分钟",theYear,theMonth,theDay,theHours,theMinute];
return theDifTimeStr;
}