UIAlertView button frames are all 0?

断了今生、忘了曾经 提交于 2019-12-22 18:35:13

问题


Is there a way for me to get a reference to the button frames in an alertView I created? I'd like to find the position of the top button so I can use its frame to position an image directly above it. I have a couple of extra \n's at the end of my alertview text to make sure there is room to place the image, but I need to determine where that free space is in the alertview. Using the top button as a reference seems like a reliable way to find it.

I have tried the following, which does return the button array (ThreePartButton??), but while the size of each button is set, the x,y origin for all of them are 0.

NSArray *buttonArray = [alertView valueForKey:@"_buttons"];
for (UIControl *aControl in buttonArray)
{
  CGRect rect = aControl.frame;
  NSLog(@"x:%2.2f y:%2.2f w:%2.2f h:%2.2f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}

Any suggestions for a working solution would be much appreciated!


回答1:


the trick is that the view's frames are computed just before showing. so use a UIAlertViewDelegate (the class where you create your UIAlertView for example) and implement the

- (void)willPresentAlertView:(UIAlertView *)alertView

delegate method. You will access the computed UIButton's frame.



来源:https://stackoverflow.com/questions/4750035/uialertview-button-frames-are-all-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!