简体   繁体   English

点击UIButton作为子视图添加到UILabel打开弹出窗口远离按钮。

[英]Clicking on UIButton added as subview to UILabel opens popup away from button.?

hello Buddies , I have used UIButton as subview to UILabel and I m opening some popup on click of that button and I found that the popup is opening away from that button What is the reason behind it , Help Please.. Thank You! 你好伙伴们,我用UIButton作为UILabel的子视图,我点击那个按钮打开一些弹出窗口,我发现弹出窗口是打开那个按钮后面的原因是什么,请帮助...谢谢! My code is as follows: 我的代码如下:

    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 Method is as follows: 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];

The reason for this is you are adding your button in categoryLabel not in view while presenting the popover in self.view. 这样做的原因是,在self.view中显示popover时,您在categoryLabel中添加按钮而不在视图中。

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). 这将获得关于self.view的按钮框架存储在矩形类型的CGRect中(框架也是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. 与呈现popover时相比,这个新的rect代替sender.frame,因为sender.frame根据categoryLabel有x,y。

hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM