【问题标题】:How to resize UIView frame during button click?如何在按钮单击期间调整 UIView 框架的大小?
【发布时间】:2016-07-26 09:49:34
【问题描述】:

我正在使用两个按钮 button1 和 button2 和一个 view1,当我单击 button2 时,view1 框架应该升高到一定高度,当按下 button2 时,view1 应该被释放到正常位置(原始位置),可以请帮忙写代码。

- (IBAction)button2:(id)sender {

   [liftView setFrame:CGRectMake(2.0, 3.0, 200.0, 10.0)];
}
- (IBAction)button1:(id)sender {

   //here i need the view back to the original position
}

【问题讨论】:

  • 你能显示你的代码吗?通过编辑将它们放入您的问题中。
  • 好的。我现在添加
  • @raptor 我添加了代码。
  • 我注意到您标记了旧版本的 iOS。你在使用自动布局吗?如果是,setFrame: 将不起作用。您应该改为调整自动布局约束
  • 不,我没有使用自动布局..

标签: ios iphone ios7 ios5


【解决方案1】:

由于没有使用Auto Layout,所以setFrame:可以用来调整布局如下:

CGRect originalFrame;

- (IBAction)button2:(id)sender {
   originalFrame = liftView.frame;
   [liftView setFrame:CGRectMake(2.0, 3.0, 200.0, 10.0)];
}
- (IBAction)button1:(id)sender {
   // set the view back to the original position
   [liftView setFrame:originalFrame];
}

这里我使用一个变量originalFrame来存储原始帧大小。

(可选)您可以将setFrame: 包裹在UIAnimation 中,以便布局更改可以动画化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2012-03-23
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多