在 iOS 开发中遇到的问题(零)

  • 界面返回之后刷新 UITableView 数据,或者刷新指定行的数据,列表出现滚动,原因是重写了 UITableView 的下面这个方法,去掉就OK了。Stack Overflow
1
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
  • UISearchBar 的结果列表,想要高度和 UITableView 的 Cell 高度一致的话,这样设置就行了:
1
self.searchDisplayController.searchResultsTableView.rowHeight = self.tableView.rowHeight;
  • 出现 Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Could not find a storyboard named ‘Main’ in bundle NSBundle <*.app> (loaded)’,原因是删掉了 Main.storyboard,但是还需要从 Info.plst 里面点掉对应的字段:
1
Remove the "Main storyboard file base name" or "UIMainStoryboardFile" Key from your info.plist file.
1
cell.selectionStyle = UITableViewCellSelectionStyleNone;
  • UIlabel layer.cornerRadius not working in iOS 7.1,解决办法:Set the property clipsToBounds to YES。Stack Overflow

  • 隐藏导航栏上的返回按钮。Stack Overflow

1
self.navigationItem.hidesBackButton = YES;
1
2
UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}];
1
2
3
4
5
6
7
8
9
10
- (void)someMethodWhereYouSetUpYourObserver {
// This could be in an init method.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationMethod:) name:UIKeyboardDidShowNotification object:nil];
}

- (void)myNotificationMethod:(NSNotification*)notification {
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
}

不要忘记在 viewWillDisappear 方法里面移除观察:

1
[[NSNotificationCenter defaultCenter] removeObserver:self];
  • 设置 SearchBar TextField 颜色:
1
2
3
4
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
searchField.textColor = [UIColor whiteColor];
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.searchBar setImage:[UIImage imageNamed:@"nav_search_btn_bg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
1
cell.textLabel.highlightedTextColor = [UIColor redColor];
1
[cell.textLabel setHighlighted:YES];
  • 在头文件中使用 @property 定义了一个可变对象,如下,通过上一个界面传过来,在使用的时候,当改变其值的时候,会出现崩溃,原因是 copy 这个属性,将这个属性去掉行OK了。
1
@property (nonatomic, copy) NSMutableDictionary *userRegisterInfo;
1
2
3
4
5
6
7
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; {
if ([text isEqualToString:@"\n"]) {
//Do whatever you want
}

return YES;
}
  • UITableView 多自定义 Cell 类型。Stack Overflow

  • 在 Storyboard 中设置 Tab Bar Item 的 Selected Image 没有用,提示:CUICatalog: Invalid asset name supplied: (null)。Stack Overflow

  • iOS UITableView first section header not show。Stack Overflow,原因:It wasn’t showing because i hadn’t implemented heightForHeaderInSection

  • 设置 UITabBar shadow backgroundimage。Stack Overflow

1
2
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
  • 设置导航栏 left Bar Button Item,如果是想要干掉返回按钮,并且使右滑返回失效,就直接设置 leftBarButtonItem。如果是想增加一个按钮,则:
1
2
self.navigationItem.leftItemsSupplementBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStylePlain target:self action:@selector(action)];

Google 搜索关键字:ios set navigation bar left button item back button disappear
Stack Overflow

  • 比较 版本号 字符串时,普通的比较在某些情况下是有问题的,因为默认是一个一个字符比较的,所以在比较版本号的字符串时,需要指定一下 option:NSNumericSearch,如下:
1
[str0 compare:str1 options:NSNumericSearch]

比如当 1.1.11.1.12 比较的时候,默认的方法是返回降序的,如果是用上面的方法,返回是升序。

  • 离线推送的 DeviceToken 转字符串

Google 搜索关键字:ios device token to string
Stack Overflow