11

我已经看到了一些解决方案,但没有一个对我的问题有效。

UIButton通过简单地拖放到UIViewController情节提要编辑器中创建了一个。

有一个链接到与该视图关联的 .h 文件的UIButton出口。UIViewController它也在.m文件中被合成。

@property (strong, nonatomic) IBOutlet UIButton *answerButton;

我想像这样在运行时更改按钮的位置;

CGRect btFrame = answerButton.frame;
btFrame.origin.x = xOne;
btFrame.origin.y = yOne;
answerButton.frame = btFrame;

但是,每当我尝试此操作时,按钮都会拒绝移动。

所有其他编辑功能(如setTitle等)都可以使用,但由于某种原因,框架不会按我想要的方式移动。

4

3 回答 3

19

Simply uncheck "Use Autolayout" in the file inspector..

于 2013-05-06T11:47:40.027 回答
7

.h 文件

    #import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    IBOutlet UIButton *theButton;
}
@property(nonatomic, strong) IBOutlet UIButton *theButton;
-(IBAction)moveTheButton:(id)sender;

@end

.m 文件

-(IBAction)moveTheButton:(id)sender{
CGRect btFrame = theButton.frame;
btFrame.origin.x = 90;
btFrame.origin.y = 150;
theButton.frame = btFrame;

}

此代码将按钮从一个点移动到另一个点。

于 2012-12-10T10:17:24.830 回答
4

用下面的代码替换您的代码,其中包括删除自动调整大小掩码的代码。

CGRect btFrame = answerButton.frame;
btFrame.origin.x = xOne;
btFrame.origin.y = yOne;
answerButton.autoresizingMask = UIViewAutoresizingNone;
answerButton.frame = btFrame;
于 2012-12-10T09:40:43.047 回答