在键盘上添加按钮
//直接将按钮添加到虚拟键盘上面
//通常会使用UIToolBar类型的视图
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
bar.barStyle = UIBarStyleBlack;
//添加按钮
//空白
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
//按钮
UIBarButtonItem *doneItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)];
UIBarButtonItem *midItem = [[UIBarButtonItem alloc] initWithTitle:@"Mid" style:UIBarButtonItemStylePlain target:self action:@selector(doneAction:)];
UIBarButtonItem *doneItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(doneAction:)];
bar.items = @[doneItem2,spaceItem,midItem,spaceItem, doneItem1];
//在虚拟键盘上面添加视图
textView.inputAccessoryView = bar;
self.view.backgroundColor = [UIColor whiteColor];
导航栏按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"submitFriendCycle.png"] style:UIBarButtonItemStylePlain target:self action:@selector(submitBtnClick)];
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -12;//此处修改到边界的距离,请自行测试
沙盒目录
[NSBundle mainBundle] pathForResource:@"submitFriendCycle@2x" ofType:@"png"]
从xib加载
SubmitController * ctrl = [[SubmitController alloc]initWithNibName:@"SubmitController" bundle:nil]
异步请求数据 主线程刷新UI
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//下载数据(这里写的是使用AFNetWorking请求的数据)
dispatch_async(dispatch_get_main_queue(), ^{
//更新视图(这里写的是,要把数据显示出来)
});
});
AFNetWorking GET
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
[session GET:@"需要请求的url" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"请求成功");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"请求失败");
}];
图片的平铺拉伸
UIImage *image = [UIImage imageNamed:@""];
image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
UITextfiled输入文字属性
NSDictionary *attrsDictionary =@{
NSFontAttributeName: self.textField.font,
NSKernAttributeName:[NSNumber numberWithFloat:10.0f]//这里修改字符间距
};
self.textField.attributedText=[[NSAttributedString alloc]initWithString:text attributes:attrsDictionary];
动态计算Label高度
单行文本:
NSString *content = @"欢迎来到北京";
CGSize size =[content sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
多行文本:
UILabel *label = [UILabel new];
label.numberOfLines = 0;//多行显示,计算高度
label.font = [UIFont systemFontOfSize:14];
CGSize titleSize = [label.text boundingRectWithSize:CGSizeMake(kScreen_Width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
iOS UITabBarController切换选项卡,关闭模态视图回到主视图
从任意页面,切换选项卡
关闭模态视图回到主视图
//跳转并切换选项卡
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController popToRootViewControllerAnimated:NO];
SLTabBarController *tabC = (SLTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
tabC.selectedIndex= 1;
});
让timer不因scrollView的滚动而停止
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
imageWithContentsOfFile方法不能加载Assets.xcassets里的图片