ios 如何以编程方式移动 UIButton。目标-C Xcode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13798269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move UIButton programmatically. Objective-C Xcode
提问by SamBo
I've seen some solutions, none of which have worked for my issue.
我已经看到了一些解决方案,但没有一个对我的问题有效。
I have a UIButton
created by simply drag/dropping into the UIViewController
in Storyboard editor.
我UIButton
通过简单地拖放到UIViewController
故事板编辑器中创建了一个。
The UIButton
has an outlet linked to the .h file for the UIViewController
associated to that view. It is also Synthesized in the .m
file.
在UIButton
具有连接到.h文件的一个出口UIViewController
相关联的那个视图。它也在.m
文件中合成。
@property (strong, nonatomic) IBOutlet UIButton *answerButton;
I want to change the location of the button during run time like this;
我想在运行时像这样更改按钮的位置;
CGRect btFrame = answerButton.frame;
btFrame.origin.x = xOne;
btFrame.origin.y = yOne;
answerButton.frame = btFrame;
However whenever I try this, the button refuses to move.
但是,每当我尝试此操作时,按钮都拒绝移动。
All other editing functions (like setTitle
etc) are functional, but for some reason the frame won't move how I want it to.
所有其他编辑功能(如setTitle
等)都可以使用,但由于某种原因,框架不会按照我想要的方式移动。
回答by Muhammad Ibrahim
Simply uncheck "Use Autolayout" in the file inspector..
只需在文件检查器中取消选中“使用自动布局”即可。
回答by RKY
.h file
.h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UIButton *theButton;
}
@property(nonatomic, strong) IBOutlet UIButton *theButton;
-(IBAction)moveTheButton:(id)sender;
@end
.m file
.m 文件
-(IBAction)moveTheButton:(id)sender{
CGRect btFrame = theButton.frame;
btFrame.origin.x = 90;
btFrame.origin.y = 150;
theButton.frame = btFrame;
}
}
This code moves the button from one point to another.
此代码将按钮从一个点移动到另一个点。
回答by Paramasivan Samuttiram
Replace your code by the below, which includes code to remove auto resizing mask.
用下面的代码替换您的代码,其中包括删除自动调整大小掩码的代码。
CGRect btFrame = answerButton.frame;
btFrame.origin.x = xOne;
btFrame.origin.y = yOne;
answerButton.autoresizingMask = UIViewAutoresizingNone;
answerButton.frame = btFrame;