iOS开发总结(二)——菜鸟之路的开始
12 获取键盘高度 keyboard
// 获取键盘高度- (CGFloat)getKeyboardHeight:(NSNotification *)notif
{
// the keyboardHeight defalut to 216
// Need observe UIKeyboardDidHideNotification or UIKeyboardDidShowNotification likes this:
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
// notif: the argument pass to method which as a selector in above like - (void)keyboardDidHide:(NSNotification *)notif
NSDictionary *dicForInfo = [notif userInfo];
NSValue *aValue = [dicForInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
return [aValue CGRectValue].size.height; // 获取的是 Keyboard 的 frame,所以是 CGRectValue
}
// 获取一个 view 和 superView 两者底部的距离
- (CGFloat)getBottomDistanceFromView:(UIView *)view
{
// get the distance between view and superView
if (view)
{
UIView *superView = [view superview];
if (superView)
{
CGRect viewFrame = view.frame;
return superView.frame.size.height - (viewFrame.origin.x + viewFrame.size.height);
}
else
NSLog(@"view has not superView");
}
else
NSLog(@"view is nil");
return -1;
}
13 运行时区分模拟器和真机的宏
模拟器 TARGET_IPHONE_SIMULATOR
#ifdef TARGET_IPHONE_SIMULATOR
// Here the simulator code
#else
// Here the iPhone code
#endif
14 ScrollView 内容小于 view 的大小时,让界面滑动
界面的滑动:contentSize 要 大于 frame.size 才会滑动
15 NSSet 不单比较内存地址,还比较内容
不单比较内存地址,还比较内容
NSString *hisName = @"Robert"; NSString *hisLastName = @"Kiyosaki";
NSString *herName = @"Kim";
NSString *herLastName = @"Kiyosaki";
NSSet *setOfNames = [[NSSet alloc] initWithObjects: hisName,
hisLastName, herName,
herLastName, nil];
NSLog(@"Set = %@", setOfNames);
You can see that the last name Kiyosaki was added only once to the list. Our set rejected the second addition of the same object to the list. It is very important to understand that a set doesn't just do a comparison on where in memory an object sits, but it actually looks into its contents.
你将看到 Kiyosaki 这个姓氏在 NSSet 的列表中只添加了一次。NSSet 拒绝另一个相同的对象添加到其中。理解下面这点很重要:一个集合(set)不单只比较对象的地址,还比较它们的内容。
16 libsqlite3.0.dylib and libsqlite3.dylib
When add library libsqlite into project, there are two libsqlite, one is libsqlite3.dylib, and the other is libsqlite3.0.dylib.
In the finder, you can see libsqlite3.0.dylib is a alias kind, and point to libsqlite3.dylib.
But which one should we use?
Someone in StackOverFlow said
"Generally speaking, unless you have specific needs that are tied to the exact version of libsqlite3, you should use libsqlite3.dylib when linking."
from: http://stackoverflow.com/questions/4314866/what-is-the-difference-between-libsqlite3-dylib-and-libsqlite3-0-dylib
当在项目中添加 libsqlite 库时,有两个 libsqlite,一个是 libsqlite3.dylib,另一个是 libsqlite3.0.dylib。
在 finder 中查看这两个库,你可以看到 libsqlite3.0.dylib 是一种别名类型,并且指向 libsqlite3.dylib。
但是我们改选择哪一个呢?
在 StackOverFlow 上有人回答道:
“一般来说,除非你有特别的需求绑定了特定的 libsqlite3 版本,否则当你链接时应该使用 libsqlite3.dylib。”
17 获取控件的值发生变化后的值
使用 UIControl 的以下方法获取
[controller addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
控件都是继承自 UIControl 。
ps:当你找不到获取控件变化的值时,可以用这个试下(UISlider、UISegment等都是如此)。
18 设置 UILabel 的行数 numberOfLines = 0 的情况
In some situations, you might not know how many lines is required to display a certain text in a label. In those instances, you need to set the numberOfLines property of your label to 0.
在某些情况下,你可能不知道在一个 lable 中显示文字需要多少行数。这种情况下,你需要设置 numberOfLiness 属性为 0.
And then, you can use - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode; to calculate the size of font which you used, so you can set your label's size.
然后,你可以通过 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode; 计算出相应 font 下的宽度和高度,设置 label 的大小。
19 UIImageView 锁定高宽比
使添加的图片的高宽比保持不变,需设置该属性
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
也可以设置以下枚举值 UIViewContentModeScaleAspectFill,图片会铺满 imageView。但是 图片会超出 imageView 的范围(如果图片高宽比和其不一致的话)。此时可设置
self.myImageView.clipsBounds = YES;
截掉超过的部分。
20 CGRect 获取高度和宽度
CGRectGetWidth
CGRectGetHeight
And CGRectGetXXX, read the document.