xcode 你能在IB中建立一个按钮并以编程方式移动它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3782039/
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-09-14 19:47:03  来源:igfitidea点击:

Can you establish a button in IB and move it programmatically?

iphoneobjective-cxcodeipad

提问by emachine

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 设置和链接到它的屏幕对象。有没有办法以编程方式更改按钮位置和/或大小?我知道您可以更改标题和某些内容,但我不知道如何更改它的位置或大小。

Thank You.

谢谢你。

回答by cobbal

You can change the frameof the button (or any UIView).

您可以更改frame按钮(或任何 UIView)的 。

CGRect frame = [button frame];
frame.origin.x += 100;  // change the location
frame.size.width += 100;  // change the size
[button setFrame:frame];

回答by MishieMoo

You simply create a new frame for it i.e. myButton.frame = CGRectMake(0,0,123,412);and it moves to that new frame. The order for CGRectMake is (origin x, origin y, width, height).

您只需为它创建一个新框架,即myButton.frame = CGRectMake(0,0,123,412);它会移动到该新框架。CGRectMake 的顺序是(原点 x,原点 y,宽度,高度)。