How can I position a button on my view dynamically?
-
Any button, or a specific button? – occulus Mar 24 '11 at 09:53
-
And do you want a pointer to the UIButton, or the location of it in the view, etc.? Be more specific in your questions. – occulus Mar 24 '11 at 09:55
-
How can i locate a button on my view dynamically? mean any button – GameLoading Mar 24 '11 at 09:56
-
For all I know, he's asking how to place a button at a specific coordinate at runtime somewhere on the view. – occulus Mar 24 '11 at 09:57
-
@fasttrack:true actually i need to give a animation effect so it make user seem like buttons are rotating around a point – rptwsthi Mar 24 '11 at 09:58
-
@rptwsthi Please edit your question then, and clarify it. People are giving you answers to something you're not interested in because your question wasn't clear. – occulus Mar 24 '11 at 10:09
4 Answers
Try this code
for(UIView * view in self.view.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
// view is button
}
}
You can also use tags

- 2,344
- 3
- 18
- 27
-
-
in that case, he wants a specific component from the view.subviews( no need to differentiate it as button), in that case tag will be only option. :) – Vaibhav Tekam Mar 24 '11 at 09:54
Set the tag
property of the UIButton (e.g. in interface builder, or programatically). Then you can walk the children of the owning view:
for (UIView *view in myView.subviews) {
if (view.tag == 101 /*or whatever you set it to*/) {
// things...
}
}

- 16,959
- 6
- 53
- 76
Get a array of all the subviews of a view using:
[myView subviews]
iterate through that array and check for your butotn by some unique characteristic. (You can set the button's tag attribute to some unique value and check for that value)

- 11,254
- 3
- 32
- 60
Ok, now we understand the question better...
You can place a button/UIComponent at runtime by settings its frame
. This will place it at a given coordinate within the view containing the button.
myButton.frame = CGRectMake(newX, newY, myButton.frame.width, myButton.frame.height);
If you wish to do a linear style animation (no curves), look up CATransitions
, see iPhone UIView Animation Best Practice. If you need to support pre-iOS4 devices, you can't use CATransitions
, and have to do it the old way using UIView
animations.
To do more complex animations, you might have to mess around with timers, which can slightly juddery effects if you're trying to do complicated animation paths.
-
thanks.. finally.. actually i'm very new to iPhone development thus i don't exactly know how to ask :P – rptwsthi Mar 24 '11 at 10:19