iOS - 开发
1. Xcode在真机调试的时候出现”The identity used to sign the executable is no longer valid”
解决方案:
(1)provisioning Profile文件选择错误
(2)provisioning Profile已失效
(3)开发者证书过期或者被删除了
2. 将storyBoard里的控制器进行实例化
注:需要在Storyboard里面设置控制器的Storyboard ID
//Identifier:@“Storyboard ID”
//将storyBoard实例化 (1)(可以在AppDelegate里面使用)
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//将要跳转控制器通过storyBoard实例化
ViewController *VC = [mainSB instantiateViewControllerWithIdentifier:@"ViewController"];
VC.isModifyPassword = NO;
[self.navigationController pushViewController:VC animated:YES];
//将storyBoard实例化 (2)
//将要跳转控制器通过storyBoard实例化
ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
VC.isModifyPassword = NO;
[self.navigationController pushViewController:VC animated:YES];
3. 仿安卓Toast
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//声明定义一个ToastLabel对象
@interface ToastLabel : UILabel
- (void)setMessageText:(NSString *)text;
@end
@interface Toast : NSObject
{
ToastLabel *toastLabel;
NSTimer *countTimer;
NSInteger changeCount;
}
//创建声明单例的方法
+ (instancetype)shareInstance;
- (void)makeToast:(NSString *)message duration:(CGFloat )duation;
@end
#import "Toast.h"
@implementation ToastLabel
/**
* ToastLabel初始化,位label设置各种属性
*
* @return ToastLabel
*/
- (instancetype)init{
self = [super init];
if (self) {
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor blackColor];
self.numberOfLines = 0;
self.textAlignment = NSTextAlignmentCenter;
self.textColor = [UIColor whiteColor];
self.font = [UIFont systemFontOfSize:15];
}
return self;
}
/**
* 设置显示的文字
*
* @aram text 文字文本
*/
- (void)setMessageText:(NSString *)text{
[self setText:text];
CGRect rect = [self.text boundingRectWithSize:CGSizeMake(DeviceWidth - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil];
CGFloat width = rect.size.width + 20;
CGFloat height = rect.size.height + 20;
CGFloat x = (DeviceWidth - width) / 2;
CGFloat y = DeviceHight - height - 80;
self.frame = CGRectMake(x, y, width, height);
}
@end
@implementation Toast
/**
* 实现声明单例的方法 GCD
*
* @return 单例
*/
+ (instancetype)shareInstance{
static Toast *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[Toast alloc] init];
});
return singleton;
}
/**
* 初始化方法
*
* @return 自身
*
*/
- (instancetype)init{
self = [super init];
if (self) {
toastLabel = [[ToastLabel alloc] init];
countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
countTimer.fireDate = [NSDate distantFuture];//关闭定时器
}
return self;
}
/**
* 弹出并显示Tosat
*
* @param message 显示的文本内容
* @param duration 显示时间
*/
- (void)makeToast:(NSString *)message duration:(CGFloat)duation{
if ([message length] == 0) {
return;
}
[toastLabel setMessageText:message];
[[[UIApplication sharedApplication]keyWindow ] addSubview:toastLabel];
toastLabel.alpha = 0.8;
countTimer.fireDate = [NSDate distantPast];//开启定时器
changeCount = duation;
}
/**
* 定时器回调方法
*/
- (void)changeTime{
//NSLog(@"时间:%ld",changeCount);
if (changeCount-- <= 0) {
countTimer.fireDate = [NSDate distantFuture];//关闭定时器
[UIView animateWithDuration:0.2f animations:^{
toastLabel.alpha = 0;
} completion:^(BOOL finished) {
[toastLabel removeFromSuperview];
}];
}
}
@end
//duration:显示的时长
[[Toast shareInstance] makeToast:@"你提示的内容" duration:1.0];
4. UITextField的左视图设置
//1.创建UITextField类别
#import <UIKit/UIKit.h>
@interface loginUITextField : UITextField
@end
//2.重写UITextField的左视图的Bounds
#import "loginUITextField.h"
@implementation loginUITextField
- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
CGRect rect = [super leftViewRectForBounds:bounds];
if (self.window.frame.size.width == 320) {
rect = CGRectMake(rect.origin.x + 3, rect.origin.y , 28, 28);
} else if (self.window.frame.size.width == 375){
rect = CGRectMake(rect.origin.x + 4, rect.origin.y , 32, 32);
} else {
rect = CGRectMake(rect.origin.x + 5, rect.origin.y , 35, 35);
}
return rect;
}
@end
//以下是例子 并不能直接使用
//_userTextField的左视图
UIImageView *useImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user"]];
_userImageView.contentMode = UIViewContentModeScaleAspectFit;
_userTextField.leftView = useImageView;
_userTextField.leftViewMode = UITextFieldViewModeAlways;
_userTextField.borderStyle = UITextBorderStyleRoundedRect;
//修改水印字体颜色和字体大小
[_userTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
[_userTextField setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
//_passwordTxetField的左视图
UIImageView *passwdImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"locked"]];
//passwdImageView.frame = CGRectMake(0, 0, 37, 37);
_passwordTxetField.contentMode = UIViewContentModeScaleAspectFit;
_passwordTxetField.leftView = passwdImageView;
_passwordTxetField.leftViewMode = UITextFieldViewModeAlways;
_passwordTxetField.borderStyle = UITextBorderStyleRoundedRect;
//修改水印字体颜色和字体大小
[_passwordTxetField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
[_passwordTxetField setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
//注:如果是在xcode8上面 需要在设置替换左视图的imageView的frame
//如下,打开passwdImageView.frame的注释(因为如果图片太大 左视图在xcode8上面会变形)
//_passwordTxetField的左视图
UIImageView *passwdImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"locked"]];
passwdImageView.frame = CGRectMake(0, 0, 37, 37);
_passwordTxetField.contentMode = UIViewContentModeScaleAspectFit;
_passwordTxetField.leftView = passwdImageView;
_passwordTxetField.leftViewMode = UITextFieldViewModeAlways;
_passwordTxetField.borderStyle = UITextBorderStyleRoundedRect;
5. 自定义NavgationBar的标题和返回按钮无文字的样式和设置NavgationBar的背景图
//自定义NavgationBar的标题
- (void)creatdTitle{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];
label.text = @"标题";
label.font = [UIFont boldSystemFontOfSize:20];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
self.navigationItem.titleView = label;
}
//返回按钮无文字的样式
- (void)createdbackBarButtonItem{
UIBarButtonItem *newBarButtonItem = [[UIBarButtonItem alloc] init];
newBarButtonItem.title = @"";
self.navigationItem.backBarButtonItem = newBarButtonItem;
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}
//设置NavgationBar的背景图 (jpg记得加后缀)
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"1"] forBarMetrics:UIBarMetricsDefault];
6. 自定义UIAlertController的标题、内容、和按钮的字体样式
- (void)created{
NSString *str = @"注 销";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:str];
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, str.length)];
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, str.length)];
[alertController setValue:alertControllerStr forKey:@"attributedMessage"];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[cancelAction setValue:[UIColor grayColor] forKey:@"titleTextColor"];
[alertController addAction:confirmAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
7. 验证码倒计时60S(注意UIButton样式不能用系统的会闪)
//self.verificationBtn:改成你自己的命名的button
- (void)startTime{
__block int timeout = 59; //倒计时时间
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0){ //倒计时结束,关闭
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置(倒计时结束后调用)
[self.verificationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
[self.verificationBtn setBackgroundColor:[UIColor colorWithRed: 0/256.0 green: 111/256.0 blue: 255/256.0 alpha:1]];
//设置可点击
self.verificationBtn.userInteractionEnabled = YES;
});
}else{
// int minutes = timeout / 60; //这里注释掉了,这个是用来测试多于60秒时计算分钟的。
int seconds = timeout % 60;
NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
//NSLog(@"____%@",strTime);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[self.verificationBtn setTitle:[NSString stringWithFormat:@"%@秒重新发送",strTime] forState:UIControlStateNormal];
[UIView commitAnimations];
self.verificationBtn.backgroundColor = [UIColor lightGrayColor];
//设置不可点击
self.verificationBtn.userInteractionEnabled = NO;
});
timeout--;
}
});
dispatch_resume(_timer);
}
8. 各种切圆角(基本上能切得都这样切)
//头像图片切圆
_userImageView.layer.cornerRadius = _userImageView.frame.size.width / 2;
_userImageView.layer.masksToBounds = YES;
//
_textFiled.layer.cornerRadius = 10;
_textFiled.layer.masksToBounds = YES;
//
view.layer.cornerRadius = 20;
view.layer.masksToBounds = YES;
9.避免程序因为NSNull而导致奔溃
//创建个类别
#import <Foundation/Foundation.h>
@interface NSNull (IsNull)
@end
#import "NSNull+IsNull.h"
@implementation NSNull (IsNull)
- (void)forwardInvocation:(NSInvocation *)invocation
{
if ([self respondsToSelector:[invocation selector]]) {
[invocation invokeWithTarget:self];
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature *sig = [[NSNull class] instanceMethodSignatureForSelector:selector];
if(sig == nil) {
sig = [NSMethodSignature signatureWithObjCTypes:"@^v^c"];
}
return sig;
}
@end
10.AFNetworking的一个小问题(获取数据的类型不对时,进行更改)
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",@"text/html",@"image/jpg",nil];