1:打开Xcode,然后闪退,报加载某库出现异常
如/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib
这个是Xcode的bug,我的情况是打开某个项目会出现这个问题
解决办法:这还是缓存问题,详见第五点(、XCode5由于缓存问题引起了很多奇怪的问题,所以清除缓存有时会起到大作用:),清除缓存再运行,一切正常。
进入步骤,按option键进入资源库,Developer—xcode—DerivedData把里面的文件清除就可以
2:加快CocoaPod安装插件的速度
(这样也解决一些插件下载时失败的问题,因为一些源代码的内容是不必要,而提供的地址却连接不到,导致报错,而无法更新)
$ pod install --no-repo-update
3:userInteractionEnabled属性
当视图对象的userInteractionEnabled设置为NO的时候,用户触发的事件,如触摸,键盘等,将会被该视图忽略(其他视图照常响应),并且该视图对象也会从事件响应队列中被移除。 当userInteractionEnabled设为YES时,则事件可以正常的传递给该视图对象。 userInteractionEnabled的属性默认值就是YES。
EaseUserHeaderView *headerView = [[EaseUserHeaderView alloc] init];
headerView.userInteractionEnabled = YES;
4:字符串显示问题,同一行不同的效果
调用:
[_fansCountBtn setAttributedTitle:[self getStringWithTitle:@"粉丝" andValue:_curUser.fans_count.stringValue] forState:UIControlStateNormal]; 方法:
- (NSMutableAttributedString*)getStringWithTitle:(NSString *)title andValue:(NSString *)value{
NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@", value, title]];
[attriString addAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:],
NSForegroundColorAttributeName : [UIColor whiteColor]}
range:NSMakeRange(, value.length)]; [attriString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:],
NSForegroundColorAttributeName : [UIColor whiteColor]}
range:NSMakeRange(value.length+, title.length)];
return attriString;
} 另:label可以调用sizeToFit来调整内容适应 _userLabel.text = _curUser.name;
[_userLabel sizeToFit];
5:NSArray中使用firstObject和lastObject
NSArray有lastObject方法用来获取数组最后一个对象,firstObject来获取数组第一个对象,NSArray中使用firstObject和lastObject的好处在于,当数组为空时,返回nil,而不会报错。当数组为空,使用myArray[]时,会报错。 NSNumber *firstLevel = nil, *secondLevel = nil;
NSArray *locationArray = [_curUser.location componentsSeparatedByString:@" "];
if (locationArray.count == ) {
firstLevel = [AddressManager indexOfFirst:[locationArray firstObject]];
secondLevel = [AddressManager indexOfSecond:[locationArray lastObject] inFirst:[locationArray firstObject]];
}
6:日期控件ActionSheetDatePicker 多级控件ActionSheetStringPicker
小实例:
NSDate *curDate = [NSDate dateFromString:_curUser.birthday withFormat:@"yyyy-MM-dd"];
if (!curDate) {
curDate = [NSDate dateFromString:@"1990-01-01" withFormat:@"yyyy-MM-dd"];
}
ActionSheetDatePicker *picker = [[ActionSheetDatePicker alloc] initWithTitle:nil datePickerMode:UIDatePickerModeDate selectedDate:curDate doneBlock:^(ActionSheetDatePicker *picker, NSDate *selectedDate, id origin) {
NSString *preValue = weakSelf.curUser.birthday;
weakSelf.curUser.birthday = [selectedDate string_yyyy_MM_dd];
[weakSelf.myTableView reloadData];
[[Coding_NetAPIManager sharedManager] request_UpdateUserInfo_WithObj:weakSelf.curUser andBlock:^(id data, NSError *error) {
if (data) {
weakSelf.curUser = data;
}else{
weakSelf.curUser.birthday = preValue;
}
[weakSelf.myTableView reloadData];
}];
} cancelBlock:^(ActionSheetDatePicker *picker) {
NSLog(@"%@", picker.description);
} origin:self.view];
picker.minimumDate = [[NSDate date] offsetYear:-];
picker.maximumDate = [NSDate date];
[picker showActionSheetPicker]; 小实例: [ActionSheetStringPicker showPickerWithTitle:nil rows:@[[AddressManager firstLevelArray], [AddressManager secondLevelMap]] initialSelection:@[firstLevel, secondLevel] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
NSString *preValue = weakSelf.curUser.location;
NSString *location = [selectedValue componentsJoinedByString:@" "];
weakSelf.curUser.location = location;
[weakSelf.myTableView reloadData];
[[Coding_NetAPIManager sharedManager] request_UpdateUserInfo_WithObj:weakSelf.curUser andBlock:^(id data, NSError *error) {
if (data) {
weakSelf.curUser = data;
}else{
weakSelf.curUser.location = preValue;
}
[weakSelf.myTableView reloadData];
}];
} cancelBlock:nil origin:self.view]; 小实例: [ActionSheetStringPicker showPickerWithTitle:nil rows:@[@[@"男", @"女", @"未知"]] initialSelection:@[_curUser.sex] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
NSNumber *preValue = weakSelf.curUser.sex;
weakSelf.curUser.sex = [selectedIndex firstObject];
[weakSelf.myTableView reloadData];
[[Coding_NetAPIManager sharedManager] request_UpdateUserInfo_WithObj:weakSelf.curUser andBlock:^(id data, NSError *error) {
if (data) {
weakSelf.curUser = data;
}else{
weakSelf.curUser.sex = preValue;
}
[weakSelf.myTableView reloadData];
}];
} cancelBlock:nil origin:self.view];
7:代码自动布局Masonry插件
Masonry支持哪一些属性@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline; 小实例: if (!_titleL) {
_titleL = [[UILabel alloc] init];
[self.contentView addSubview:_titleL];
_titleL.font = [UIFont systemFontOfSize:];
_titleL.textColor = [UIColor blackColor];
}
if (!_valueL) {
_valueL = [[UILabel alloc] init];
_valueL.numberOfLines = ;
[self.contentView addSubview:_valueL];
_valueL.font = [UIFont systemFontOfSize:];
_valueL.textColor = [UIColor colorWithHexString:@"0x999999"];
} [_titleL mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(kPaddingLeftWidth);
make.height.mas_equalTo();
make.top.equalTo(self.contentView).offset();
make.width.mas_equalTo(kScreen_Width - *kPaddingLeftWidth);
}];
[_valueL mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_titleL.mas_bottom).offset();
make.left.right.equalTo(_titleL);
}];
实例(注意右边跟下边要用负数):
UIView *sv=[UIView new];
sv.backgroundColor=[UIColor redColor];
[self.view addSubview:sv];
//定义一个剧中大小
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
UIView *childView=[UIView new];
childView.backgroundColor=[UIColor greenColor];
[sv addSubview:childView];
//相对父视图
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(sv.mas_left).with.offset(20);
make.top.equalTo(sv.mas_top).with.offset(10);
make.right.equalTo(sv.mas_right).with.offset(-20);
make.height.equalTo(@200);
}];
UIView *otherChildView=[UIView new];
otherChildView.backgroundColor=[UIColor yellowColor];
[sv addSubview:otherChildView];
//相对父视图跟兄弟视图
[otherChildView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(childView.mas_bottom).with.offset(10);
make.bottom.equalTo(sv.mas_bottom).with.offset(-10);
make.left.equalTo(sv.mas_left).with.offset(10);
make.width.mas_equalTo(@240);
}];
另一实例:
// 先给redBtn添加约束
[redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
// 添加左、上边距约束
// 添加上边距约束
make.top.mas_equalTo(100);
// 添加左边距约束
make.left.mas_equalTo(20);
// 添加右边距约束
make.right.mas_equalTo(-20);
}];
// 还是老规矩 先初始化一个btn
UIButton * blueBtn = [UIButton new];
blueBtn.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueBtn];
// 给蓝色btn添加约束
[blueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
// 添加右、下边距约束
// make.bottom.and.right.mas_equalTo(-20);
// 添加下边距约束
make.bottom.mas_equalTo(-20);
// 添加右边距约束
make.right.mas_equalTo(-20);
// 添加高度约束,让高度等于红色Btn
make.height.equalTo(redBtn);
// 添加上边距约束(上边距 = 红色btn的下边框 + 偏移量20)
make.top.equalTo(redBtn.mas_bottom).offset(20);
// 添加左边距(左边距 = 父容器纵轴中心 + 偏移量0)
make.left.equalTo(weakSelf.view.mas_centerX).offset(0);
}];
好文章:http://www.cocoachina.com/ios/20141219/10702.html
关于更新布局mas_updateConstraints的运用 实例一: #import "Masonry.h" @interface ViewController5() @property(strong,nonatomic)UILabel *myLabel; @end @implementation ViewController5 -(void)viewDidLoad
{
[super viewDidLoad];
self.myLabel=[UILabel new];
self.myLabel.text=@"GOOD";
self.myLabel.backgroundColor=[UIColor blueColor];
[self.view addSubview:self.myLabel]; [self.myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.mas_left).with.offset();
make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-);
make.size.mas_equalTo(CGSizeMake(, ));
}];
}
- (IBAction)actionBtn:(id)sender {
CGFloat newBottom=-;
//修改布局
[_myLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(newBottom);
}]; //要进行layoutIfNeeded 才有效果
[self.myLabel layoutIfNeeded];
}
@end 实例二:
(修改键盘跟输入框的隐藏问题)
#import "Masonry.h" @interface ViewController4 () @property (strong, nonatomic) UITextField *textField; @end @implementation ViewController4 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self];
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. __weak typeof(self) weakSelf = self; _textField = [UITextField new];
_textField.backgroundColor = [UIColor redColor];
[self.view addSubview:_textField]; [_textField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo();
make.centerX.equalTo(weakSelf.view);
make.bottom.mas_equalTo();
make.height.mas_equalTo();
}]; // 注册键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
} - (void)keyboardWillChangeFrameNotification:(NSNotification *)notification { // 获取键盘基本信息(动画时长与键盘高度)
NSDictionary *userInfo = [notification userInfo];
CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改下边距约束
[_textField mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-keyboardHeight);
}]; // 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{ [self.view layoutIfNeeded];
}];
} - (void)keyboardWillHideNotification:(NSNotification *)notification { // 获得键盘动画时长
NSDictionary *userInfo = [notification userInfo];
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改为以前的约束(距下边距0)
[_textField mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo();
}]; // 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{ [self.view layoutIfNeeded];
}];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event];
[self.view endEditing:YES];
} @end