索引
友情提示:
点击下方超链接, 开启新页面后, 会自动定位到对应的位置.
也可以直接复制想看的内容, 使用Command + F
直接搜索.
Command + F
搜索不到时, 是因为超链接是直接链接到了另外一篇文章, 请直接点击超链接进行查看.
-
NSDate
- <a href="#NSDateGetTomorrowAndYesterday" > 获取昨天和明天 </a>
- <a href="#NSDateGetTimeZoneDate" > 获取带时区的 NSDate </a>
- <a href="#NSDateGetTimeString" > 将 NSDate 转为字符串 </a>
- <a href="#NSDateGetIsSameDay" > 判断是否是同年同月同一天 </a>
-
UINavigationController
-
UITableViewController
- <a href="#UITableViewControllerSuspensionView" >在 UITableViewController 中添加一个悬浮视图 (不随 TableView 滚动)</a>
-
UISearchBar
- <a href="#UISearchBarPlaceHolderColor" >修改 UISearchBar 的 placeHolderColor</a>
- <a href="#UISearchBarTextColor" >修改 UISearchBar 的 TextColor</a>
-
UITextField
- <a href="#UITextFieldMonitorTextChange" >监听 UITextField 中的文字发生变化</a>
- <a href="#UITextFieldTextDown" >输入中文时, 文字下移</a>
-
UITextView
- <a href="#UITextViewMonitorTextChange" >监听 UITextView 中的文字发生变化</a>
-
UIStatusBar
- <a href="#UIStatusBarHide" >隐藏 UIStatusBar</a>
- <a href="#UIStatusBarHide64" >隐藏 UIStatusBar, 并保持 UINavigationBar 高度为 64</a>
-
UILabel
- <a href="#GradientLabel" >渐变色 Label</a>
-
AMap
- <a href="#AMapDistanceTwoPoint" >计算两坐标点之间距离</a>
-
Cocoapods
- <a href="#CocoapodError-36">error: RPC failed; curl 56 SSLRead() return error -36</a>
-
App Publish
- <a href="#ArchiveDisable">Archive 按钮不可点击</a>
NSDate
<a name="NSDateGetTomorrowAndYesterday" id="NSDateGetTomorrowAndYesterday" ></a>
获取昨天和明天
环境: iOS10
、Xcode8.1
NSDate *today = [NSDate date];
// 昨天
NSDate *yesterday = [NSDate dateWithTimeInterval: -24*60*60 sinceDate: today];
// 明天
NSDate *tomorrow = [NSDate dateWithTimeInterval: 24*60*60 sinceDate: today];
<a name="NSDateGetTimeZoneDate" id="NSDateGetTimeZoneDate" ></a>
获取带时区的 NSDate
环境: iOS10
、Xcode8.1
NSDate *currentDate = [NSDate date];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
NSTimeInterval timeInterval = [timeZone secondsFromGMTForDate: currentDate];
NSDate *timeZoneDate = [currentDate dateByAddingTimeInterval: timeInterval];
<a name="NSDateGetTimeString" id="NSDateGetTimeString" ></a>
将 NSDate 转为字符串
环境: iOS10
、Xcode8.1
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]];
<a name="NSDateGetIsSameDay" id="NSDateGetIsSameDay" ></a>
判断是否是同年同月同一天
环境: iOS10
、Xcode8.1
- (BOOL)isSameDay:(NSDate *)date1 date2:(NSDate *)date2 {
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
return [comp1 day] == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year] == [comp2 year];
}
UITableViewController
<a name="UITableViewControllerSuspensionView" id="UITableViewControllerSuspensionView" ></a>
在 UITableViewController 中添加一个悬浮视图 (不随 TableView 滚动)
环境: iOS10
、Xcode8.1
- 重写
UITableViewController
中的loadView
方法.
- (void)loadView {
[super loadView];
// Obtain self.view
UITableView *tableView = (UITableView *)self.view;
// Create container view
UIView *containerView = [[UIView alloc] initWithFrame: self.view.bounds];
[containerView addSubview: tableView];
// Replace self.view
self.view = containerView;
}
- 然后就可以在
self.view
中添加悬浮视图了, 例如:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc];
view.bounds = CGRectMake(0, 0, 50, 50);
view.backgroundColor = [UIColor greenColor];
[self.view addSubview: view];
}
UISearchBar
<a name="UISearchBarPlaceHolderColor" id="UISearchBarPlaceHolderColor" ></a>
修改 UISearchBar 的 placeHolderColor
环境: iOS10
、Xcode8.1
UISearchBar *searchBar = [UISearchBar new];
UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
searchTextField.textColor = [UIColor blackColor];
<a name="UISearchBarTextColor" id="UISearchBarTextColor" ></a>
修改 UISearchBar 的 TextColor
环境: iOS10
、Xcode8.1
UISearchBar *searchBar = [UISearchBar new];
UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
[searchTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
UITextField
<a name="UITextFieldMonitorTextChange" id="UITextFieldMonitorTextChange" ></a>
监听 UITextField 中的文字发生变化
环境: iOS10
、Xcode8.1
- 注册系统通知:
textDidChangeNotificationAction
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(textDidChangeNotificationAction:)
name: UITextFieldTextDidChangeNotification
object: nil];
- 实现响应方法:
textDidChangeNotificationAction:
- (void)textDidChangeNotificationAction:(NSNotification *)notification {
UITextField *textField = notification.object; // 通过 notification.object, 获取对应的 TextField
}
<a name="UITextFieldTextDown" id="UITextFieldTextDown" ></a>
输入中文时, 文字下移
环境: iOS10
、Xcode8.1
问题现象: 这个 Bug
最常见的情况是在 UITableViewController
中, 当你设置 TextField
的 borderStyle
属性为 UITextBorderStyleNone
的时候, 当你点击 TextField
准备输入文字时, 就会复现这个 Bug
. 看下面两张图, 第一张为普通状态, 第二张为输入状态:
很明显看到 小
字向下偏移了. (Note: 这个问题, 貌似是当 ViewController
的 View
的第一个子视图是 ScrollView
或 ScrollView
子类时, 都会出现这个情况, 有兴趣的同学可以试试.)
解决办法: 首先设置 borderStyle
属性为 UITextBorderStyleLine
, 然后在照一张纯白色的图片, 当做 TextField
的背景(这张图片可以使代码生成的, 也可以是一张 1*1
像素的纯白图片). Storyboard
的设置如下图:
UITextView
<a name="UITextViewMonitorTextChange" id="UITextViewMonitorTextChange" ></a>
监听 UITextView 中的文字发生变化
环境: iOS10
、Xcode8.1
- 注册系统通知:
textDidChangeNotificationAction
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(textDidChangeNotificationAction:)
name: UITextViewTextDidChangeNotification
object: nil];
- 实现响应方法:
textDidChangeNotificationAction:
- (void)textDidChangeNotificationAction:(NSNotification *)notification {
UITextView *textView = notification.object; // 通过 notification.object, 获取对应的 TextView
}
UIStatusBar
<a name="UIStatusBarHide" id="UIStatusBarHide" ></a>
隐藏 UIStatusBar
环境: iOS10
、Xcode8.1
-
首先设置
info.plist
文件, 添加下面这一行.
隐藏与显示 UIStatusBar
// Show status bar
[[UIApplication sharedApplication] setStatusBarHidden: YES
withAnimation: UIStatusBarAnimationSlide];
// Hide status bar
[[UIApplication sharedApplication] setStatusBarHidden: NO
withAnimation: UIStatusBarAnimationSlide];
<a name="UIStatusBarHide64" id="UIStatusBarHide64" ></a>
隐藏 UIStatusBar, 并保持 UINavigationBar 高度为 64
环境: iOS10
、Xcode8.1
UIStatusBar 隐藏后, 会出现 UINavigationBar 上移 20 个点的情况, 解决办法如下. 查看原文
UINavigationBar+StatusBarHidden.h
@interface UINavigationBar (StatusBarHidden)
@property (nonatomic) BOOL fixedNavigationBarHeight;
@end
UINavigationBar+StatusBarHidden.m
#import <objc/runtime.h>
static char const* const FixedNavigationBarHeight = "FixedNavigationBarHeight";
@implementation UINavigationBar (StatusBarHidden)
#pragma mark - Override Methods
+ (void)load {
method_exchangeImplementations(class_getInstanceMethod(self, @selector(sizeThatFits:)),
class_getInstanceMethod(self, @selector(sizeThatFits_FixedNavigationBarHeight:)));
}
#pragma mark - Replace Methods
- (CGSize)sizeThatFits_FixedNavigationBarHeight:(CGSize)size {
if ([UIApplication sharedApplication].statusBarHidden &&
[[[UIDevice currentDevice] systemVersion] compare: @"7.0" options:NSNumericSearch] != NSOrderedAscending &&
self.fixedNavigationBarHeight) {
CGSize newSize = CGSizeMake(self.frame.size.width, 64);
return newSize;
}
else return [self sizeThatFits_FixedNavigationBarHeight:size];
}
#pragma mark - Override Getter/Setter Methods
- (BOOL)fixedNavigationBarHeight {
return [objc_getAssociatedObject(self, FixedNavigationBarHeight) boolValue];
}
- (void)setFixedNavigationBarHeight:(BOOL)fixedNavigationBarHeight {
objc_setAssociatedObject(self, FixedNavigationBarHeight,
[NSNumber numberWithBool:fixedNavigationBarHeight], OBJC_ASSOCIATION_RETAIN);
}
@end
UILabel
<a name="GradientLabel" id="GradientLabel" ></a>
渐变色 Label
环境: iOS10
、Xcode8.1
-
先看效果
- 生成一个
UILabel
的成员变量
@interface ViewController () {
UILabel *_lblTitle;
}
- 初始化
_lblTitle
:
_lblTitle = [UILabel new];
_lblTitle.text = @"Liguoan";
_lblTitle.textAlignment = NSTextAlignmentCenter;
_lblTitle.font = [UIFont systemFontOfSize: 20];
- 初始化
CAGradientLayer
实例:
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.position = self.view.center;
gradientLayer.bounds = (CGRect){CGPointZero, {[UIScreen mainScreen].bounds.size.width, 44}};
gradientLayer.colors = @[
(id)[UIColor redColor] .CGColor,
(id)[UIColor orangeColor].CGColor,
(id)[UIColor greenColor] .CGColor
];
gradientLayer.startPoint = CGPointMake(0, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);
[self.view.layer addSublayer: gradientLayer];
- 重点: 将
_lblTitle.layer
设置成gradientLayer
的maskLayer
:
gradientLayer.mask = _lblTitle.layer;
_lblTitle.frame = gradientLayer.bounds;
AMap
<a name="AMapDistanceTwoPoint" id="AMapDistanceTwoPoint" ></a>
计算两坐标点之间距离
环境: iOS10
、Xcode8.1
- 生成
MAMapPoint
对象
MAMapPoint point1 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(39.989612,116.480972));
MAMapPoint point2 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(39.990347,116.480441));
- 计算两点之间距离
CLLocationDistance distance = MAMetersBetweenMapPoints(point1,point2);
Cocoapods
<a name="CocoapodError-36" id="CocoapodError-36" ></a>
error: RPC failed; curl 56 SSLRead() return error -36
问题现象: 如下图
遇到这个错误一般来说是因为你电脑里安装了两个及两个以上的 Xcode 导致的.
解决办法: 此时在终端输入以下指令:
sudo xcode-select -switch /Applications/Xcode8.app/Contents/Developer
路径有可能不相同, 所以需要在Applications
中找到 Xcode
,右键显示包内容,找到 Developer
文件夹拖到终端里面即可.
App Publish
<a name="ArchiveDisable" id="ArchiveDisable" ></a>
Archive 按钮不可点击
解决办法: 不要使用模拟器, 需要选择成 Device
.