你好伙伴们,我已经使用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];
sorttheData方法如下:
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];
答案 0 :(得分:2)
原因是您在self.view中显示popover时,在categoryLabel中添加按钮而不在视图中。
尝试以下:
CGRect rect = [self.view convertRect:[sortBtn frame] fromView:self.view];
[popController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
这将获得关于self.view的按钮框架存储在矩形类型的CGRect中(框架也是CGRect的类型)。与呈现popover时相比,这个新的rect代替sender.frame,因为sender.frame根据categoryLabel有x,y。
希望这有助于:)