版权声明:本文为博主原创文章,未经博主允许不得转载。
[objc] view plain copy
指定根视图
[objc] view plain copy
- self.window.rootViewController = [RootViewController new];
方法实现:
[objc] view plain copy
- #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];
- }
[objc] view plain copy
- - (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;
- }];
- }
释放:
[objc] view plain copy
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
- }
最终效果:
有问题可以关注我微博私信我.http://weibo.com/hanjunqiang
原文地址:http://blog.csdn.net/qq_31810357/article/details/49611281
时间: 2024-11-05 06:22:53