版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
指定根視圖
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
cgrect frame = self.textfield.frame;
frame.origin.y = kscreenheight - 100;
釋放:
- (void)dealloc
[[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboardwillhidenotification object:nil];
[[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboardwillshownotification object:nil];
最終效果:

原文位址:http://blog.csdn.net/qq_31810357/article/details/49611281