iOS 根据父视图布局的方法 以及实现 Scrollview 拉动 遮罩输入框问题解决

来源:互联网 发布:windows loader 2.1.6 编辑:程序博客网 时间:2024/05/21 06:29

1. iOS 根据父视图布局设定对应的x y点,然后设置当前视图的所在位置根据比例设置。

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



0 0
原创粉丝点击