查看调整大小不适用于第一次viewDidAppear调用

时间:2014-10-08 06:54:42

标签: ios objective-c uiview

我正在更改UIView旁边viewDidAppear的框架。

这是我的viewDidAppear代码

-(void)viewDidAppear:(BOOL)animated
{  
   CGRect newPosition =CGRectMake(44, 200, 697, 142);//hard code for testing purpose.
   myView.frame = newPosition;  //here i'm moving view to new position
}

viewDidAppear myView的第一次通话仍在旧位置。 但是在viewDidAppear的第二次调用中,myView移至newPosition;

为什么我的观点在第一次调用viewDidAppear时没有转移到newPosition?

3 个答案:

答案 0 :(得分:0)

您好忘了[super viewDidAppear:animated];

    -(void)viewDidAppear:(BOOL)animated
    {  
      [super viewDidAppear:animated];

       CGRect newPosition =CGRectMake(44, 200, 697, 142);//hard code for testing purpose.
       myView.frame = newPosition;  //here i'm moving view to new position
    }

但是为了更好地改变框架:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    CGRect newPosition =CGRectMake(44, 200, 697, 142);//hard code for testing purpose.
    myView.frame = newPosition;  //here i'm moving view to new position
}

同时尝试添加小动画以进行帧更改它会更酷。

[UIView animateWithDuration:0.3 animations:^{
        CGRect newPosition =CGRectMake(44, 200, 697, 142);//hard code for testing purpose.
        myView.frame = newPosition;  //here i'm moving view to new position
        } completion:^(BOOL finished) {
        }];

最终代码将是:

 - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];

        [UIView animateWithDuration:0.3 animations:^{
            CGRect newPosition =CGRectMake(44, 200, 697, 142);//hard code for testing purpose.
            myView.frame = newPosition;  //here i'm moving view to new position
            } completion:^(BOOL finished) {
            }];
    }

答案 1 :(得分:0)

尝试在layoutSubviews

中执行此操作

示例:

- (void) layoutSubviews
{
    [super layoutSubviews];

   CGRect newPosition =CGRectMake(44, 200, 697, 142);//hard code for testing purpose.
   myView.frame = newPosition;  //here i'm moving view to new position
}

答案 2 :(得分:0)

考虑到所有其他答案,我认为原因是当最初调用viewDidAppear时myView为nil,而后来调用时则不为nil:

- (void)viewDidAppear:(BOOL)animated
{ 
   [super viewDidAppear:animated]; // was missing

   CGRect newPosition = CGRectMake(44, 200, 697, 142);
   myView.frame = newPosition; // nil.frame = ...
}

你应该在这里调用super的实现,但这不应该导致这样的问题。

另一个可能的问题是,某些内容导致myView在第一个-viewDidAppear:之后被重新定位,所以你定位它,再次重新定位它,你可以调试这个覆盖-setFrame: myView或某种方式使用符号断点。