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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:28:50  来源:igfitidea点击:

How to move UIButton programmatically. Objective-C Xcode

iosobjective-cxcodeuibutton

提问by SamBo

I've seen some solutions, none of which have worked for my issue.

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

I have a UIButtoncreated by simply drag/dropping into the UIViewControllerin Storyboard editor.

UIButton通过简单地拖放到UIViewController故事板编辑器中创建了一个。

The UIButtonhas an outlet linked to the .h file for the UIViewControllerassociated to that view. It is also Synthesized in the .mfile.

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 setTitleetc) 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;