【问题标题】:Cannot resize UIScrollView's Frame无法调整 UIScrollView 的 Frame 大小
【发布时间】:2014-04-15 12:59:53
【问题描述】:

我有一个 UIScrollView,它的 frame 和 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;
    }];
}

我没有使用Autolayout,无论我是否使用动画,都会出现同样的问题。

【问题讨论】:

  • 您是否尝试将 contentSize 与框架一起更改?
  • @dariaa 但我希望内容大小保持不变,因此孔屏可以滚动:/

标签: ios iphone uiscrollview resize frame


【解决方案1】:

尝试更改keyboaardDidAppear 通知上的content size

- (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;
}

【讨论】:

  • 如果我只是改变内容大小,它会起作用,但如果我也尝试改变框架,什么都不会发生。
  • 能否提供全班代码。我想某些方法可能会将contentSize 设置回框架。
  • 我会,但这是一个非常大的类哈哈,我搜索了,我没有在其他任何地方设置它。奇怪的行为是,如果更改位置下方的框架,或内容大小,修改代码,则不起作用,无论位置修改,还是内容大小或框架。如果我删除框架修改代码,位置或内容大小修改工作。 o.O
  • 见我上面的代码。它在没有动画的情况下对我有用。
  • @6rod9 :我更新了代码,现在它适用于动画。请检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
相关资源
最近更新 更多