iOS 根据父视图布局的方法 以及实现 Scrollview 拉动 遮罩输入框问题解决
2014-11-22 13:58
399 查看
1. iOS 根据父视图布局设定对应的x y点,然后设置当前视图的所在位置根据比例设置。
2. iOS 通过ScrollView 设置时限拉动效果 iOS 7.0 不需要减掉 顶部状态栏的高度 20, 但是7.0 以后需要减掉状态栏
3. iOS 遮罩文本框问题。
2. iOS 通过ScrollView 设置时限拉动效果 iOS 7.0 不需要减掉 顶部状态栏的高度 20, 但是7.0 以后需要减掉状态栏
3. iOS 遮罩文本框问题。
// // SecondController.m // HappyApp // // Created by LiShijia on 14/11/19. // Copyright (c) 2014年 com.johnli. All rights reserved. // #import "SecondController.h" #import "ViewController.h" #import <UIKit/UIKit.h> @implementation SecondController -(void) viewDidLoad{ [self.view setBackgroundColor:[UIColor whiteColor]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil]; // CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView) // [self.navigationController.toolbar setHidden:YES]; _scroller = [[UIScrollView alloc ] initWithFrame: self.view.bounds]; _scroller.frame = self.view.frame; NSLog(@"before return the height %.f",_scroller.frame.size.height); // [_scroller setBackgroundColor: [UIColor redColor]]; CGSize size; size = self.view.frame.size; size.height = self.view.frame.size.height - 44.f - 19.f; _scroller.contentSize = size; NSLog(@"the frame bounds %f",self.navigationController.toolbar.frame.size.height); _scroller.bounces = YES; [self.view addSubview: _scroller]; [self addUser]; [self addPwd]; [self addPopButton]; } //can key when you click return or finish -(void) backUpTap{ [self.view endEditing:YES]; } - (void) textFieldDidBeginEditing:(UITextField *)textField{ float viewY = textField.bounds.origin.y; // //// self.view.window.bounds // _scroller.contentSize = CGSizeMake(rect.size.width,rect.size.height + 10); // //原始滑动距离增加键盘高度 // CGPoint pt = [textField convertPoint:CGPointMake(0, 0) toView:_scroller]; // //把当前的textField的坐标映射到scrollview上 // float navBarheight = self.navigationController.navigationBar.bounds.size.height; NSLog(@"offset y %f",_scroller.contentOffset.y); NSLog(@"view y %f",viewY); NSLog(@"nav bar height %f",navBarheight); if(_scroller.contentOffset.y-viewY+navBarheight<=0){ // //判断最上面不要去滚动 //// [_scroller setContentOffset:CGPointMake(0, viewY-navBarheight) animated:YES]; [_scroller setContentOffset:CGPointMake(0, viewY-navBarheight) animated:YES]; } } //- (void) textFieldDidBeginEditing:(UITextField *)textField{ //// foucsTextField = textField; // float viewY = textField.bounds.origin.y; // //// self.view.window.bounds // CGRect rect = [UIScreen mainScreen].applicationFrame; // _scroller.contentSize = CGSizeMake(rect.size.width,rect.size.height +216); // //原始滑动距离增加键盘高度 // CGPoint pt = [textField convertPoint:CGPointMake(0, 0) toView:_scroller]; // //把当前的textField的坐标映射到scrollview上 // // float navBarheight = self.navigationController.navigationBar. //// if(_scroller.contentOffset.y-viewY+navBarheight<=0){ // //判断最上面不要去滚动 //// [_scroller setContentOffset:CGPointMake(0, viewY-navBarheight) animated:YES]; // //[_scroller setContentOffset:CGPointMake(0, viewY-navBarheight) animated:YES]; //// } // //} // - (void) keyboardWillShow { // NSLog(@"the keyboard will show %@",nofication); NSLog(@" the keyboard will show"); // CGPoint offet = CGPointMake(0, self.view.bounds.origin.y + 216); // // float offetHeigh = _userPwd.bounds.origin.y - 216; // // NSLog(@"offer %f",_userPwd.bounds.origin.y); // [_scroller setContentOffset:offet animated:YES]; } - (void) addUser{ //获取父视图对象的 x 以及 y 点 然后计算当前视图所排放的位置 CGRect urect = CGRectMake(_scroller.center.x * 0.2f, _scroller.center.y * 0.3f, 100, 50 ); UILabel * userLabel = [[UILabel alloc] initWithFrame:urect]; [userLabel setText:@"UserName"]; [_scroller addSubview:userLabel]; // NSLayoutConstraint *layout = [NSLayoutConstraint constraintWithItem:userLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_scroller attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0]; // NSLog(@"ssssssss %.f",userLabel.frame.origin.y); urect.origin.x = _scroller.center.x * 0.8f; urect.origin.y = _scroller.center.y * 0.35f; urect.size.height = 20; urect.size.width = _scroller.frame.size.width * 0.54f;; UITextField * userField = [[UITextField alloc] initWithFrame: urect]; [userField setBackgroundColor:[UIColor redColor]]; userField.borderStyle = UITextBorderStyleRoundedRect; // [userField addTarget:self action:@selector(backUpTap) forControlEvents:UIControlEventEditingDidEndOnExit]; userField.delegate = self; [_scroller addSubview:userField]; } //add user pwd lable -(void) addPwd{ CGRect prect = CGRectMake(_scroller.center.x * 0.2f, _scroller.center.y * 0.5f, 150, 50 ); UILabel * pwdLable = [[UILabel alloc] initWithFrame:prect]; [pwdLable setText:@"Password"]; [_scroller addSubview:pwdLable]; prect.origin.x = _scroller.center.x; prect.origin.y = _scroller.center.y * 0.55f; prect.size.height = 20; prect.size.width = 100; _userPwd = [[UITextField alloc] initWithFrame:prect]; [_userPwd setBackgroundColor:[UIColor redColor]]; _userPwd.secureTextEntry = YES; _userPwd.borderStyle = UITextBorderStyleRoundedRect; _userPwd.delegate = self; // [userPwd addTarget:self action:@selector(backUpTap) forControlEvents:UIControlEventEditingDidEndOnExit]; [_scroller addSubview:_userPwd]; } - (void) addPopButton{ UIButton * popButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 100, 50)]; [popButton setBackgroundColor:[UIColor redColor]]; [popButton setTitle:@"Login" forState:UIControlStateNormal]; [popButton addTarget:self action:@selector(popLastController) forControlEvents:UIControlEventTouchUpInside]; [_scroller addSubview:popButton]; } -(BOOL) textFieldShouldReturn:(UITextField *) textField { NSLog(@"you are clicing the return key"); [self.view endEditing:YES]; //开始动画 [UIView beginAnimations:nil context:nil]; //设定动画持续时间 [UIView setAnimationDuration:0.3]; _scroller.frame = self.view.frame; NSLog(@"after return the height %.f",_scroller.frame.size.height); CGSize size; size = self.view.frame.size; size.height = self.view.frame.size.height - 44.f - 19.f; // [_scroller setContentSize: size]; //动画结束 [UIView commitAnimations]; return YES; } - (void) popLastController{ //[self.navigationController popViewControllerAnimated:YES]; //ViewController * vc = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0]; [self.navigationController popToRootViewControllerAnimated:YES]; // [self.navigationController popToViewController:vc animated:YES]; } @end
相关文章推荐
- iOS项目开发实战——制作视图的平移动画以及解决移动异常问题
- IOS开发键盘弹出时遮住输入框的问题的一种解决方法。
- iOS地图定位功能的实现(解决代理方法不执行问题,解决默认定位苹果总部问题)
- 在xib和story board上实现代码自动布局(解决添加约束时,大于等于小于等于繁琐的问题,以及占位view的问题)
- 【iOS开发-113】在storyboard上用AutoLayout,纯代码实现AutoLayout布局方法以及简单动画
- ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)
- socket iOS 与java 后台进行socket通讯遇到的问题以及解决方法
- socket iOS 与java 后台进行socket通讯遇到的问题以及解决方法
- socket iOS 与java 后台进行socket通讯遇到的问题以及解决方法
- iOS工作中的问题-----navigationBar透明方法、遮挡UIViewController、UITableViewController 视图解决方法
- adaboost 算法在实现中的一些问题以及解决方法(持续更新)
- 【iOS开发-113】在storyboard上用AutoLayout,纯代码实现AutoLayout布局方法以及简单动画
- socket iOS 与java 后台进行socket通讯遇到的问题以及解决方法
- socket iOS 与java 后台进行socket通讯遇到的问题以及解决方法
- IOS 子视图touch,父视图Ges手势冲突问题解决方法
- 记录IOS开发中遇到的问题以及解决方法
- Mac中用Ant实现Android的批量打包碰到的一些问题以及解决方法
- socket iOS 与java 后台进行socket通讯遇到的问题以及解决方法
- socket iOS 与java 后台进行socket通讯遇到的问题以及解决方法
- ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)