简体   繁体   English

如何动态更改UIButton的位置

[英]How to change the position of a UIButton dynamically

I have a group of 31 buttons in a IBOutletCollection, 我在IBOutletCollection中有一组31个按钮,

This is to create a calendar (ie) the number of days in a month, I have to change the position of these buttons depending on the 1st days week day, that looks like a calendar. 这是为了创建日历(即一个月中的天数),我必须根据工作日的第一天(即日历)来更改这些按钮的位置。

I am not able to add target-action mechanism when I write code to implement the same in Xcode 4.2, so I tried do'g it in the Interface Builder using IBOutlet Collection. 当我编写代码以在Xcode 4.2中实现相同功能时,我无法添加目标动作机制,因此我尝试使用IBOutlet Collection在Interface Builder中对其进行操作。

But now the question is how do I change the postion of these bottons each one representing the one day of the month? 但是现在的问题是,如何更改这些代表月中某一天的各个botton的位置?

You can change the position like so: 您可以像这样更改位置:

myButton.frame = CGRectMake( x, y, width, height );

eg. 例如。

myButton.frame = CGRectMake( 20, 20, 100, 50 );

I'm not sure exactly what your calendar is for - but you might want to check out an open source iPhone calendar, it's pretty good: http://cocoacontrols.com/platforms/ios/controls/kal 我不确定您的日历的确切用途-但您可能想查看一个开源iPhone日历,它非常不错: http : //cocoacontrols.com/platforms/ios/controls/kal

You could either just use it as it is, or look at how it's implemented and use that to help you develop yours. 您可以按原样使用它,或者查看它的实现方式并使用它来帮助您开发自己的产品。

For changing the position, use the center property of that button. 要更改位置,请使用该按钮的center属性。

For example: 例如:

theButton.center = CGPointMake(10.0f, 10.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM