IOS开发基础知识--碎片17
1:contentSize、contentInset和contentOffset区别
contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个scrollview,它的frame为(,,,),而它的contentSize为(,).也就是说,这个scrollview整个内容的大小为(,),要通过上下滑动scrollview来查看(,)后的内容。 contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是( ,-),也就是y偏移了480 contentInset 是scrollview中contentView.frame.origin与scrollview.frame.origin的关系,比如contentView的frame为(,,,),那么contentInset则为(, ),它也可以设置上下左右
2:IOS虚拟器安装其它Simulator
下载后的dmg安装.这里主要以iOS7.0模拟器的离线安装为例进行说明,其他版本以此类推: 下载ios_7_0_simulator.dmg后打开dmg文件,可以看到安装包iPhoneSimulatorSDK7_0.pkg,使用安装器安装此安装包,默认会安装在所选分区的/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7..sdk目录下,完全退出Xcode后将刚才安装的iPhoneSimulator7..sdk整个目录复制或移动到/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs目录下即可,(Xcode.app右键可以"显示包内容“)重新启动Xcode一般就可以使用相应版本的模拟器进行开发和调试了。 离线安装还有一个简单的办法就是将以前安装过的旧版本的Xcode如Xcode5..2下面已经安装好了的iOS模拟器直接复制过来使用,目录位置都一样,都是在Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs里面。这样就不用再下载离线安装包了。
3:输入框中的inputaccessoryview和inputview
UITextFields和UITextView有一个inputAccessoryView的属性,当你想在键盘上展示一个自定义的view时,你就可以设置该属性。你设置的view就会自动和键盘keyboard一起显示了。需要注意的是,你所自定义的view既不应该处在其他的视图层里,也不应该成为其他视图的子视图。其实也就是说,你所自定义的view只需要赋给属性inputAccessoryView就可以了,不要再做其他多余的操作。 inputview则是键盘视图,当其为nil时则弹出的是系统默认的键盘; 实例一(给键盘上方设置一个工具条的方式): - (void)createKeyboardTool
{
keyboardTool = [[UIToolbar alloc] initWithFrame: CGRectMake(kZero, kZero, kScreenW, 44.0f)];
NSMutableArray *myToolBarItems = [NSMutableArray array]; //创建键盘工具条上面的按钮,并设置点击事件
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(cancelAction)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(saveAction)];
UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(saveAction)]; [myToolBarItems addObject:cancelBtn];
[myToolBarItems addObject:space];
[myToolBarItems addObject:saveBtn];
keyboardTool.items = myToolBarItems;
} //inputAccessoryView:设置键盘顶部显示的工具条;inputView:自定义键盘
commentTextView = [[UITextView alloc]initWithFrame:CGRectMake(kZero, kZero, kScreenW, )];
[commentTextView becomeFirstResponder];
commentTextView.inputAccessoryView = keyboardTool; 实例二(修改键盘视图,进行切换自定义视图跟系统自带视图): /**
* 切换键盘
*/
- (void)switchKeyboard
{
// self.textView.inputView == nil : 使用的是系统自带的键盘
if (self.textView.inputView == nil) {
// 切换为自定义的表情键盘 emtionKeyboard为一个视图
self.textView.inputView = self.emotionKeyboard;
// 显示键盘按钮
self.toolbar.showKeyboardButton = YES;
} else {
// 切换为系统自带的键盘
self.textView.inputView = nil;
// 显示表情按钮
self.toolbar.showKeyboardButton = NO;
}
// 开始切换键盘 这个是为固定用的
self.switchingKeybaord = YES;
// 退出键盘
[self.textView endEditing:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 弹出键盘,让其慢点实现
[self.textView becomeFirstResponder];
// 结束切换键盘
self.switchingKeybaord = NO;
});
} /**
* 键盘的frame发生改变时调用(显示、隐藏等)
*/
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
// 如果正在切换键盘,就不要执行后面的代码
if (self.switchingKeybaord) return; NSDictionary *userInfo = notification.userInfo;
// 动画的持续时间
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 键盘的frame
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 执行动画
[UIView animateWithDuration:duration animations:^{
// 工具条的Y值 == 键盘的Y值 - 工具条的高度
if (keyboardF.origin.y > self.view.height) { // 键盘的Y值已经远远超过了控制器view的高度
self.toolbar.y = self.view.height - self.toolbar.height;
} else {
self.toolbar.y = keyboardF.origin.y - self.toolbar.height;
}
}];
}
4:修改UISearchBar中关于cannel取消的文字
-(UISearchBar *)mySearchBar
{
if (_mySearchBar==nil) {
_mySearchBar=[[UISearchBar alloc]init];
_mySearchBar.showsCancelButton=YES;
_mySearchBar.delegate=self;
[_mySearchBar sizeToFit];
[_mySearchBar setPlaceholder:@"请输入"];
[_mySearchBar setY:]; //处理cannel的文字显示
for (id item in [_mySearchBar subviews]) {
for(id cc in [item subviews])
{
if ([cc isKindOfClass:[UIButton class]]) {
UIButton *btn=(UIButton *)cc;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}
return _mySearchBar;
} 如果是获得瞧点才显示出取消可以在这个委托里面进行设置: /**
* @author wujunyang, 15-06-24 11:06:44
*
* @brief 修改cancel的显示文字 必先把showscancelButton设置为yes
* @param searchBar <#searchBar description#>
*/
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton=YES;
for (id item in [searchBar subviews]) {
for(id cc in [item subviews])
{
if ([cc isKindOfClass:[UIButton class]]) {
UIButton *btn=(UIButton *)cc;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}
还有另外一种方式:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
searchController.searchBar.showsCancelButton = YES;
UIButton *canceLBtn = [searchController.searchBar valueForKey:@"cancelButton"];
[canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
[canceLBtn setTitleColor:[UIColor colorWithRed:14.0/255.0 green:180.0/255.0 blue:0.0/255.0 alpha:1.00] forState:UIControlStateNormal];
searchBar.showsCancelButton = YES;
return YES;
}
5:关于navigationController中增加控件时push跳转及跳回
在子页navigationController增加控件,回跳时它是没办法自个销除,所以要手动增加一个销除nav所增加的控件,否则子页的那个控件会被重叠显示在父页的nav上;如下一个实例: 在viewDidLoad里
//加载控件
[self.navigationController.view addSubview:self.mySearchBar]; (void)viewWillDisappear:(BOOL)animated {
//这句也可以写在回跳前
[self.mySearchBar removeFromSuperview];
[super viewWillDisappear:animated];
}
6:整个视图点击都对键盘进行收缩
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGr=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
//如果没有这句在view中的Button等可能无法触发ToucheUpInside事件
tapGr.cancelsTouchesInView=NO;
[self.view addGestureRecognizer:tapGr];
}
- (IBAction)BtnAction:(id)sender {
NSLog(@"%@",self.myTextField.text);
}
-(void)viewTapped:(UITapGestureRecognizer *)tapGr
{
[self.myTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
7:针对第三方插件为mrc,而工程为arc的调用
对第三方插件的.m文件进行设置,工程targets-build phases-compile sources 设置-fno-objc-arc 有些是双星如 PLTexture **previewTextures;
在arc下面则要修改成:PLTexture * __unsafe_unretained *previewTextures;
8:通知的方式实现键盘的收缩布局问题
/**
* 添加工具条
*/
- (void)setupToolbar
{
// 1.添加工具条
IWComposeToolbar *toolbar = [[IWComposeToolbar alloc] init];
toolbar.delegate = self;
CGFloat toolbarH = ;
CGFloat toolbarW = self.view.width;
CGFloat toolbarY = self.view.height - toolbarH;
toolbar.frame = CGRectMake(, toolbarY, toolbarW, toolbarH);
[self.view addSubview:toolbar];
self.toolbar = toolbar; // 2.监听键盘的弹出和隐藏
// 键盘的frame(位置)即将改变, 就会发出UIKeyboardWillChangeFrameNotification
// 键盘即将弹出, 就会发出UIKeyboardWillShowNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 键盘即将隐藏, 就会发出UIKeyboardWillHideNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
} #pragma mark - 键盘处理
/**
* 键盘即将隐藏
*/
- (void)keyboardWillHide:(NSNotification *)note
{
// 1.键盘弹出需要的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 2.动画
[UIView animateWithDuration:duration animations:^{
self.toolbar.transform = CGAffineTransformIdentity;
}];
} /**
* 键盘即将弹出
*/
- (void)keyboardWillShow:(NSNotification *)note
{
// 1.键盘弹出需要的时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 2.动画
[UIView animateWithDuration:duration animations:^{
// 取出键盘高度
CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardH = keyboardF.size.height;
self.toolbar.transform = CGAffineTransformMakeTranslation(, - keyboardH);
}];
} //通知要销掉
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} 注意:[self.textView resignFirstResponder];放弃瞧点
还有可以监听输入内容的变化: // 2.监听textView文字的改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:textView];
9:封装一个uivew带有按键工具栏的实例
.h文件内容:
#import <UIKit/UIKit.h>
@class IWComposeToolbar;
typedef enum {
IWComposeToolbarButtonTypeCamera,
IWComposeToolbarButtonTypePicture,
IWComposeToolbarButtonTypeMention,
IWComposeToolbarButtonTypeTrend,
IWComposeToolbarButtonTypeEmotion
} IWComposeToolbarButtonType;
@protocol IWComposeToolbarDelegate <NSObject>
@optional
- (void)composeToolbar:(IWComposeToolbar *)toolbar didClickButton:(IWComposeToolbarButtonType)butonType;
@end
@interface IWComposeToolbar : UIView
@property (weak, nonatomic) id<IWComposeToolbarDelegate> delegate;
@end
.m文件内容:
#import "IWComposeToolbar.h"
@implementation IWComposeToolbar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 1.设置背景
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
// 2.添加按钮
[self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:IWComposeToolbarButtonTypeCamera];
[self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:IWComposeToolbarButtonTypePicture];
[self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:IWComposeToolbarButtonTypeMention];
[self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:IWComposeToolbarButtonTypeTrend];
[self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:IWComposeToolbarButtonTypeEmotion];
}
return self;
}
- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(IWComposeToolbarButtonType)tag
{
UIButton *button = [[UIButton alloc] init];
button.tag = tag;
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
[button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
[self addSubview:button];
}
/**
* 监听按钮点击
*/
- (void)buttonClick:(UIButton *)button
{
if ([self.delegate respondsToSelector:@selector(composeToolbar:didClickButton:)]) {
[self.delegate composeToolbar:self didClickButton:button.tag];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
int count = self.subviews.count;
CGFloat buttonW = self.width / count;
CGFloat buttonH = self.height;
for (int i = ; i<count; i++) {
UIButton *button = self.subviews[i];
CGFloat buttonX = buttonW * i;
button.frame = CGRectMake(buttonX, , buttonW, buttonH);
}
}
@end
IOS开发基础知识--碎片17的更多相关文章
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- IOS开发基础知识--碎片33
1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...
- IOS开发基础知识--碎片42
1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...
- IOS开发基础知识--碎片50
1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...
- IOS开发基础知识--碎片3
十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...
- IOS开发基础知识--碎片11
1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...
- IOS开发基础知识--碎片14
1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...
- IOS开发基础知识--碎片16
1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOO ...
- IOS开发基础知识--碎片19
1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...
随机推荐
- 了解HTML锚点
概念 <a>元素 (或HTML锚元素, Anchor Element)通常用来表示一个锚点/链接.但严格来说,<a>元素不是一个链接,而是超文本锚点,可以链接到一个新文件.用i ...
- javascript面向对象系列第二篇——创建对象的5种模式
× 目录 [1]字面量 [2]工厂模式 [3]构造函数[4]原型模式[5]组合模式 前面的话 如何创建对象,或者说如何更优雅的创建对象,一直是一个津津乐道的话题.本文将从最简单的创建对象的方式入手,逐 ...
- Objective-C中小怪兽的逻辑
学习Objective-C的面向对象也有一段时间了,为了犒劳自己的学习成果,写个小怪兽来犒劳一下自己把.在LOL中有怪兽和英雄的角色吧,接下来就先写一个小怪兽的类吧.从小怪兽的角度来讲,怪兽都有那些行 ...
- 怎样录制屏幕并将结果保存为Gif
怎样录制屏幕 并将结果保存为GIF 大前天写文章,需要把PPT的翻转页面截成动态图.我一开始就想到保存文件肯定是GIF.但是如何生成呢?素材又从哪里来?以前自己感兴趣做过把一组连拍的图片做成动态图,再 ...
- C++面向对象
此博文仅作为C++考研专业课的复习内容. 面向对象 构造函数 在对象被创建的时候将自动调用. 复制构造函数 形参是本类对象的引用.其作用是使用一个已经存在的对象,去初始化一个同类的新对象. 复制构造函 ...
- 1Z0-053 争议题目解析704
1Z0-053 争议题目解析704 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 704.View the Exhibit and examine the data manipul ...
- Node.js入门初体验
今天有一个类似网络爬虫的需求,本来打算用我还算熟悉的asp或者asp.NET来做这个事情,但是写了这么长时间js,asp的语法实在不喜欢,VS又早被我卸掉了,思来想去打算用一下最近比较火的Node.j ...
- PHP面试题之实现输出100以内的质数
最近求职时的其中一道面试题: 求100之内的质数 <? //求100以内质数 for ($i = 1; $i <= 100; $i++) { $k = 0; for ($j = 1; $j ...
- AngularJs $q promise
angularjs提供的$q服务是对Promises规范的一个实现.$q服务可以把一段异步的代码封装成同步的样式. 为啥是样式,因为异步还是异步,它并不会柱塞代码,只是看起来像同步代码. $q.whe ...
- 客户端调用 WCF 的几种方式
转载网络代码.版权归原作者所有..... 客户端调用WCF的几种常用的方式: 1普通调用 var factory = new DataContent.ServiceReference1.Custome ...