如何以编程方式移动UIButton.Objective-C Xcode

Sam*_*mBo 11 xcode objective-c uibutton ios

我见过一些解决方案,其中没有一个能解决我的问题.

UIButton只需拖放到UIViewControllerStoryboard编辑器中即可创建.

UIButton具有连接到.h文件的一个出口UIViewController相关联的那个视图.它也是.m文件中的Synthesized .

@property (strong, nonatomic) IBOutlet UIButton *answerButton;
Run Code Online (Sandbox Code Playgroud)

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

CGRect btFrame = answerButton.frame;
btFrame.origin.x = xOne;
btFrame.origin.y = yOne;
answerButton.frame = btFrame;
Run Code Online (Sandbox Code Playgroud)

但是每当我尝试这个时,按钮就会拒绝移动.

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

Muh*_*him 19

只需在文件检查器中取消选中"使用Autolayout"即可.


小智 7

.h文件

    #import <UIKit/UIKit.h>

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

@end
Run Code Online (Sandbox Code Playgroud)

.m文件

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

}

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