我见过一些解决方案,其中没有一个能解决我的问题。
我只需拖放到Storyboard编辑器中的UIButton
即可创建UIViewController
。
UIButton
的插座已链接到与该视图关联的UIViewController
的.h文件。它也是.m
文件中的Synthesized。
@property (strong, nonatomic) IBOutlet UIButton *answerButton;
我想在运行时更改按钮的位置,如下所示;
CGRect btFrame = answerButton.frame;
btFrame.origin.x = xOne;
btFrame.origin.y = yOne;
answerButton.frame = btFrame;
然而,每当我尝试这个时,按钮就会拒绝移动。
所有其他编辑功能(如setTitle
等)功能正常,但由于某种原因,框架不会按我想要的方式移动。
答案 0 :(得分:19)
只需在文件检查器中取消选中“使用Autolayout”..
答案 1 :(得分: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;
}
此代码将按钮从一个点移动到另一个点。
答案 2 :(得分:4)
用下面的代码替换你的代码,其中包括删除自动调整大小掩码的代码。
CGRect btFrame = answerButton.frame;
btFrame.origin.x = xOne;
btFrame.origin.y = yOne;
answerButton.autoresizingMask = UIViewAutoresizingNone;
answerButton.frame = btFrame;