I've run into an issue that is driving me nuts... I know it must be a simple fix, but I cannot find anything online (but !might be looking in the wrong places).
I have a bunch of buttons that I place in the view with a loop:
//make the button smaller开发者_如何学Go and position
CGRect buttonFrame = button.frame;
buttonFrame.size = CGSizeMake(41, 41);
buttonFrame.origin = CGPointMake(10+(43*i), 415);
button.frame = buttonFrame;
This worked great...but now I want to add them into another view and they disappear. The offending line of code is:
buttonFrame.origin = CGPointMake(10+(43*i), 415);
I believe that this must be working off of the coordinates of self.view and not the new subview that holds the buttons. Do I need a new CGContext for the super view.
I tried something like:
buttonFrame.origin = [newView CGPointMake(10+(43*i), 415)];
But that caused all sorts of errors.
Thanks for your time!
The origin of the CGRect that is assigned as the frame of a view is the offset that the view will be placed at from it's superview.
So if you use a CGRect of (100, 100, 200, 200), the view will be 100 pixels to the right of the origin, and 100 pixels south (also remember that the iOS view coordinate system origin is in the top left corner) and has a size of 200x200.
Your button will be 415 pixels below the local origin (top left corner) of it's superview, which may place it out of view.
buttonFrame.origin = [newView CGPointMake(10+(43*i), 415)]; - this line of code has no sense, because CGPointMake is the C function, and not the ObjectiveC method, so you can't post it like the message to object. If you want to add subview you have to use [newView addSuview: button]; and make sure you've added your view to another view.
精彩评论