0

你好,伙计们,我已经使用 UIButton 作为 UILabel 的子视图,我在单击该按钮时打开了一些弹出窗口,我发现弹出窗口正在远离该按钮打开它背后的原因是什么,请帮助.. 谢谢!我的代码如下:

    sortBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 [sortBtn setTitle:@"Sort" forState:UIControlStateNormal];
 [sortBtn setBackgroundColor:[UIColor colorWithRed:0.137 green:0.384 blue:0.871 alpha:1.0]];
 [sortBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
 [sortBtn addTarget:self action:@selector(sortTheData:) forControlEvents:UIControlEventTouchUpInside];

  sortBtn.frame=CGRectMake(195,5, 53, 28);

 [categoryLabel setUserInteractionEnabled:YES];
 [categoryLabel addSubview:sortBtn];

排序数据方法如下:

SortByController *sortController = [[SortByController alloc] initWithNibName:@"SortByController" bundle:nil];
sortController.dataArray = self.dataAllPerformances;
sortController.sortString =self.sortString;
sortController.genreId = [NSString stringWithFormat:@"%d",self.genreId];
sortController._delegate = self;
self.popController = [[UIPopoverController alloc] initWithContentViewController:sortController];
//popController.delegate = self;
popController.popoverContentSize = CGSizeMake(230, 260);
[popController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[sortController release];
4

1 回答 1

2

The reason for this is you are adding your button in categoryLabel not in view while presenting the popover in self.view.

try following:

CGRect rect = [self.view convertRect:[sortBtn frame] fromView:self.view];
[popController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

this will get your button's frame in respect to self.view storing in rect type of CGRect (frame is also type of CGRect). Than while presenting popover pass this new rect in place of sender.frame as sender.frame has its x,y according to the categoryLabel.

hope this helps :)

于 2012-05-17T12:56:31.600 回答