我有一个UIScrollView与它的框架和contentSize具有相同的大小。当键盘出现时,我想改变scrollView的帧高度,但它不起作用。如果我改变x或y的位置,一切正常,但如果我试图改变框架的宽度或高度,一切保持不变,什么也没有发生。
下面是我的代码:
-(void)resizeContentView:(BOOL)resize forOffset:(CGFloat)offset
{
[UIView animateWithDuration:0.3f animations:^{
UIView* resizeView = [self getResizingView];
if ( !resizeView )
return;
CGRect rect = resizeView.frame;
if (resize)
{
rect.size.height -= offset;
}
else
{
rect.size.height += offset;
}
resizeView.frame = rect;
}];
}
我没有使用自动布局,无论我是否使用动画,都会出现同样的问题。
尝试更改content size
对keyboaardDidAppear
的通知。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardWillShow:(NSNotification*) notif {
NSLog(@"-(void)keyboardWillShow");
[UIView animateWithDuration:2 animations:^{
self.scrollview.frame = CGRectMake(0, 0, self.scrollview.frame.size.width/2, self.scrollview.frame.size.height/3
);
} completion:^(BOOL finished) {
self.scrollview.backgroundColor = [UIColor redColor];
}];
}
-(void)keyboardWillHide:(NSNotification*) notif {
NSLog(@"-(void)keyboardWillHide");
self.scrollview.backgroundColor = [UIColor greenColor];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}