指定根视图
self.window.rootViewController = [RootViewController new];
方法实现:
#import "RootViewController.h" #define kScreenHeight [UIScreen mainScreen].bounds.size.height #define kScreenWidth [UIScreen mainScreen].bounds.size.width @interface RootViewController () @property (nonatomic, strong) UITextField *textField; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor greenColor]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFrame:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hiddenFrame2:) name:UIKeyboardWillHideNotification object:nil]; self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, kScreenHeight - 100, kScreenWidth - 100, 35)]; self.textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:self.textField]; }
- (void)changeFrame:(NSNotification *)sender { CGRect frame = self.textField.frame; frame.origin.y = 100; [UIView animateWithDuration:2 animations:^{ self.textField.frame = frame; }]; } - (void)hiddenFrame2:(NSNotification *)sender { [UIView animateWithDuration:2 animations:^{ CGRect frame = self.textField.frame; frame.origin.y = kScreenHeight - 100; self.textField.frame = frame; }]; }
释放:
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; }
最终效果:
有问题可以关注我微博私信我.http://weibo.com/hanjunqiang