【问题标题】:Changing the position of UIView改变 UIView 的位置
【发布时间】:2013-10-18 05:20:56
【问题描述】:

我需要设置 UIView 的位置。下面的代码显示了我是如何做到的。但它不起作用。我该怎么做?

- (void)viewDidLoad
{
  [super viewDidLoad];

      CGRect frame = myview.frame;
      frame.origin.x = myview.frame.origin.x;
      frame.origin.y =   0;
      myview.frame = frame;

NSLog(@"%d", frame.origin.y );

}

【问题讨论】:

  • 您确认myview 不是nil?

标签: objective-c cocoa-touch uiview


【解决方案1】:

UIView 有一个 center 属性..如果你想改变位置然后就使用它...而不是调整整个视图的大小

myview.center = CGPointMake(50, 250);

希望对你有帮助。

【讨论】:

    【解决方案2】:

    确保您的 OUTLET 已连接到 nib 文件中的视图。

    【讨论】:

    • 你的代码很完美,它对我有用。只需确保您的插座必须在您的 xib 文件中连接。
    • 这对您有帮助吗?
    【解决方案3】:

    尝试在 viewDidAppear 方法中执行此操作,它适用于我。

    【讨论】:

      【解决方案4】:

      移动视图/用动画改变视图位置

      如果您想根据需要更改 View 的位置,只需使用以下代码行:

      .h 文件:

      - (void)moveView:(UIView *)view withDuration:(NSTimeInterval)duration
              andCurve:(int)curve inDirection_X:(CGFloat)x inDirection_Y:(CGFloat)y;
      

      .m 文件:

      - (void)moveView:(UIView *)view withDuration:(NSTimeInterval)duration
              andCurve:(int)curve inDirection_X:(CGFloat)x inDirection_Y:(CGFloat)y
      {
          // Setup the animation
          [UIView beginAnimations:nil context:NULL];
          [UIView setAnimationDuration:duration];
          [UIView setAnimationCurve:curve];
          [UIView setAnimationBeginsFromCurrentState:YES];
      
          // The transform matrix
          CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
          view.transform = transform;
      
          // Commit the changes
          [UIView commitAnimations];
      }
      

      注意:要在任何地方调用上述方法,只要喜欢它, 根据您的要求,即如何移动视图/使用动画更改视图的位置

       [self moveView:mFrontSideView withDuration:2.0 andCurve:0 inDirection_X: 0.0  inDirection_Y: mBackSideView.center.y + 100.0];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多