您的位置:首页 > 移动开发 > IOS开发

iOS中 通知中心Text (实例)

2015-11-03 08:24 369 查看
指定根视图

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: