IOS开发基础知识--碎片17

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1:contentSize、contentInset和contentOffset区别

contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个scrollview,它的frame为(0,0,320,480),而它的contentSize为(320,960).也就是说,这个scrollview整个内容的大小为(320,960),要通过上下滑动scrollview来查看(320,480)后的内容。

contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0,-480),也就是y偏移了480

contentInset 是scrollview中contentView.frame.origin与scrollview.frame.origin的关系,比如contentView的frame为(0,30,320,480),那么contentInset则为(0,30),它也可以设置上下左右

2:IOS虚拟器安装其它Simulator

下载后的dmg安装.这里主要以iOS7.0模拟器的离线安装为例进行说明,其他版本以此类推:

下载ios_7_0_simulator.dmg后打开dmg文件,可以看到安装包iphoneSimulatorSDK7_0.pkg,使用安装器安装此安装包,默认会安装在所选分区的

/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk目录下,完全退出Xcode后将刚才安装的iPhoneSimulator7.0.sdk整个目录复制或移动到/applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs目录下即可,(Xcode.app右键可以"显示包内容“)重新启动Xcode一般就可以使用相应版本的模拟器进行开发和调试了。离线安装还有一个简单的办法就是将以前安装过的旧版本的Xcode如Xcode5.0.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,200)];

[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;//动画的持续时间doubleduration =[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];//键盘的frameCGRect 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:20];//处理cannel的文字显示for(id itemin[_mySearchBar subviews]) {for(id ccin[item subviews])

{if([cc isKindOfClass:[UIButtonclass]]) {

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 itemin[searchBar subviews]) {for(id ccin[item subviews])

{if([cc isKindOfClass:[UIButtonclass]]) {

UIButton*btn=(UIButton *)cc;

[btn setTitle:@"取消"forState:UIControlStateNormal];

}

}

}

}

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=35;

CGFloat toolbarW=self.view.width;

CGFloat toolbarY= self.view.height -toolbarH;

toolbar.frame= CGRectMake(0, toolbarY, toolbarW, toolbarH);

[self.view addSubview:toolbar];

self.toolbar=toolbar;//2.监听键盘的弹出和隐藏//键盘的frame(位置)即将改变, 就会发出UIKeyboardWillChangeFrameNotification//键盘即将弹出, 就会发出UIKeyboardWillShowNotification[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];//键盘即将隐藏, 就会发出UIKeyboardWillHideNotification[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];

}#pragmamark - 键盘处理/**

* 键盘即将隐藏*/- (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(0, -keyboardH);

}];

}//通知要销掉- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

注意:[self.textView resignFirstResponder];放弃瞧点

还有可以监听输入内容的变化://2.监听textView文字的改变[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotificationobject:textView];

9:封装一个uivew带有按键工具栏的实例

.h文件内容:

#import<UIKit/UIKit.h>@class IWComposeToolbar;

typedefenum{

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];

}returnself;

}- (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.delegaterespondsToSelector:@selector(composeToolbar:didClickButton:)]) {

[self.delegatecomposeToolbar:self didClickButton:button.tag];

}

}- (void)layoutSubviews

{

[super layoutSubviews];intcount =self.subviews.count;

CGFloat buttonW= self.width /count;

CGFloat buttonH=self.height;for(inti =0; i<count; i++) {

UIButton*button =self.subviews[i];

CGFloat buttonX= buttonW *i;

button.frame= CGRectMake(buttonX,0, buttonW, buttonH);

}

}

@end

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航