xcode UIButton 改变位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16405609/
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
UIButton Changing Position
提问by casillas
I have a button set up in IB. I have an IBOutlet set up and the onscreen object linked to it. Is there a way to programmatically change that buttons position and/or size? I know you can change the title and some things but I don't see how to change it's position or size.
我在 IB 中设置了一个按钮。我有一个 IBOutlet 设置和链接到它的屏幕对象。有没有办法以编程方式更改按钮位置和/或大小?我知道您可以更改标题和某些内容,但我不知道如何更改它的位置或大小。
Now I would like to change the position of it accordingly. Is it possible? If yes, please let me know how, since I am trying to change the position of my button in the following code, but it does not work in the header file.
现在我想相应地改变它的位置。是否可以?如果是,请告诉我如何操作,因为我正在尝试更改以下代码中按钮的位置,但它在头文件中不起作用。
@property (nonatomic, strong) IBOutlet UIButton *mybuttonOutlet;
In the implementation file:
在实现文件中:
-(void)viewDidLoad {
screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
mybuttonOutlet.frame= CGRect(38, 464 ,157,25);
if(screenSizeHeight==480)
mybuttonOutlet.frame= CGRect(38, 364 ,157,25);
}
回答by Retterdesdialogs
Remove Use Autolayout
from the button in IB or storyboard.
Use Autolayout
从 IB 或故事板中的按钮中删除。
回答by user1988
If you want to adjust positions with Autolayout enabled, you will have to change your code like this
如果要在启用自动布局的情况下调整位置,则必须像这样更改代码
-(void)viewDidLayoutSubviews {
screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
mybuttonOutlet.frame= CGRect(38, 464 ,157,25);
if(screenSizeHeight==480)
mybuttonOutlet.frame= CGRect(38, 364 ,157,25);
}
Basically you need to perform any custom layout adjustments in viewDidLayoutSubviews method if Autolayout is enabled
基本上,如果启用了 Autolayout,您需要在 viewDidLayoutSubviews 方法中执行任何自定义布局调整
回答by Ruchira Randana
Please perform this check.
请执行此检查。
-(void)viewDidLoad{
if(self.mybuttonOutlet==nil)NSLog(@"Button is NIL");
}
Now if you get the log "Button is NIL", then probably you forgot to link your IB button to your variable mybuttonOutlet.
现在,如果您得到日志“Button is NIL”,那么您可能忘记将 IB 按钮链接到变量 mybuttonOutlet。
回答by Durul Dalkanat
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, strong) IBOutlet UIButton *theButton;
// -(IBAction)moveTheButton:(id)sender;
@end
@implementation ViewController
@synthesize theButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)moveTheButton:(id)sender{
//set default position
CGRect btFrame = theButton.frame;
btFrame.origin.x = 145;
btFrame.origin.y = 285;
theButton.frame = btFrame;
//position changing
btFrame.origin.x += 40;
theButton.frame = btFrame;
//new size of button
CGRect rect=CGRectMake(145, 285, 190, 30);
[self.theButton setBounds:rect];
}
@end
回答by CularBytes
I eventually went for the constraints. Get an outlet of the constraints that determine the position of the button (top, bottom, leading, trailing) and change the constraint(s) value(s).
我最终选择了限制条件。获取确定按钮位置的约束的出口(顶部、底部、前导、尾随)并更改约束值。
self.constBtnSubmitTrailing.constraint = 50.0
This so you can keep AutoLayout enabled.
这样您就可以保持启用 AutoLayout。
回答by Maris
The solution I figured out for this is to create new View object inside the main View, and put my "dynamically changing objects" inside that View (as shown in picture).
我想出的解决方案是在主视图中创建新的 View 对象,并将我的“动态变化的对象”放在该视图中(如图所示)。
Then I use Auto-Layout to position the new View in the main View. But bugButton:UIButton which is inside the new View, changes position as expected with:
然后我使用自动布局在主视图中定位新视图。但是新视图中的 bugButton:UIButton 会按预期更改位置:
bugButton.frame.origin = CGPoint(x: newX, y: newY)