如何正确前弹出从一个用表与UIPopoverArrowDirectionRight或UIPopoverArrowDirectionLeft
-
26-09-2019 - |
题
我总是试图提出一个弹出从一个细胞内部提供这样的:
[myPopover presentPopoverFromRect:cell.frame inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
但是我不能使用UIPopoverArrowDirectionRight或左,因为,取决于位置 的(纵向或横向)、弹出现别的地方。
我呈递的弹出正确的方式?
PS:该图表是在detailView的splitView.
解决方案
下面是一个简单的解决方案,它为我
工作正常- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect rect=CGRectMake(cell.bounds.origin.x+600, cell.bounds.origin.y+10, 50, 30);
[popOverController presentPopoverFromRect:rect inView:cell permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
其他提示
您正在经由rectForRowAtIndexPath方法获得来自小区中的帧。这是对的。然而,实现代码如下是最有可能是更大的iPad上查看的子视图所以当酥料饼得到它认为他们在大图的坐标。这就是为什么酥料饼的出现在错误的地点。
实施例中,的CGRect该行是(0,40,320,44)。相反,酥料饼目标的实现代码如下该帧的它,而不是针对你的主要观点,即帧。
我通过在我的较大视图从表中的相对坐标转换帧到坐标解决了这个问题。
代码:
CGRect aFrame = [self.myDetailViewController.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:theRow inSection:1]];
[popoverController presentPopoverFromRect:[self.myDetailViewController.tableView convertRect:aFrame toView:self.view] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
希望帮助别人寻找这个问题。
我碰到这个问题走到今天,我发现一个简单的解决方案。结果 当实例化酥料饼,你需要指定单元格的内容视图:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *aViewController = [[UIViewController alloc] init];
// initialize view here
UIPopoverController *popoverController = [[UIPopoverController alloc]
initWithContentViewController:aViewController];
popoverController.popoverContentSize = CGSizeMake(320, 416);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[popoverController presentPopoverFromRect:cell.bounds inView:cell.contentView
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[aView release];
// release popover in 'popoverControllerDidDismissPopover:' method
}
在夫特,上述答案之间这适用于我在iPad在任何方位:
if let popOverPresentationController : UIPopoverPresentationController = myAlertController.popoverPresentationController {
let cellRect = tableView.rectForRowAtIndexPath(indexPath)
popOverPresentationController.sourceView = tableView
popOverPresentationController.sourceRect = cellRect
popOverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Any
}
弹出弹出下一步的辅助u可以使用这个代码:)
我使用这个更多 先进的使用:
- 找到定义accesoryView(细胞。accesoryView)
- 如果有空,找到产生accesoryView(UIButton)如果细胞了
- 如果UIButton不存在,找到电池contet视(UITableViewCellContentView)
- 如果单元contet视并不存在,使用小区图
可以使用 UIActionSheet 或 UIPopoverController.
这里是我的代码:
UIView *accessoryView = cell.accessoryView; // finds custom accesoryView (cell.accesoryView)
if (accessoryView == nil) {
UIView *cellContentView = nil;
for (UIView *accView in [cell subviews]) {
if ([accView isKindOfClass:[UIButton class]]) {
accessoryView = accView; // find generated accesoryView (UIButton)
break;
} else if ([accView isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {
// find generated UITableViewCellContentView
cellContentView = accView;
}
}
// if the UIButton doesn't exists, find cell contet view (UITableViewCellContentView)
if (accessoryView == nil) {
accessoryView = cellContentView;
}
// if the cell contet view doesn't exists, use cell view
if (accessoryView == nil) {
accessoryView = cell;
}
}
[actionSheet showFromRect:accessoryView.bounds inView:accessoryView animated:YES];
测试iOS4.3至5.1
最好使用的方法:
-(UIView*)getViewForSheetAndPopUp:(UITableViewCell*)cell;
和方法码:
-(UIView*)getViewForSheetAndPopUp:(UITableViewCell*)cell {
UIView *accessoryView = cell.accessoryView;
if (accessoryView == nil) {
UIView *cellContentView = nil;
for (UIView *accView in [cell subviews]) {
if ([accView isKindOfClass:[UIButton class]]) {
accessoryView = accView;
break;
} else if ([accView isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {
cellContentView = accView;
}
}
if (accessoryView == nil) {
accessoryView = cellContentView;
}
if (accessoryView == nil) {
accessoryView = cell;
}
}
return accessoryView;
}
我也碰到过这个问题为好。一种用于我的解决办法是简单地改变由CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
返回的矩形的宽度:
CGRect rect = [aTableView rectForRowAtIndexPath:indexPath];
//create a 10 pixel width rect at the center of the cell
rect.origin.x = (rect.size.width - 10.0) / 2.0;
rect.size.width = 10.0;
[self.addExpensePopoverController presentPopoverFromRect:rect inView:aTableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
此创建在细胞内为中心的矩形。以此方式,酥料饼具有findind良好的光点的位置,以本身更机会。
电池框架将是像0,0,宽度,尺寸,我不相信这将有它的X和相对的tableView Y ...你想用 - (的CGRect)rectForRowAtIndexPath:(NSIndexPath * )indexPath对于这一点,这应该回到你正确的框架相对的tableView细胞...这里是一个链接的 REF的UITableView
我有同样的问题,这是解决方法,我使用:
- 在我用表,我加了行动(IBActions为我产生我的细胞是从一笔),用于电池的特定按钮。
- 然后我定义的一个CellActionDelegate协议的,可以模仿我的行动选择,而我有我的按钮(发件人)和我的小区(自我)
- 然后detailViewController我splitViewController实现这一协议,把从该单元的其坐标...
这里的一个例子码
在MyCustomTableViewCell.m:
-(IBAction)displaySomeCellRelativePopover:(id)sender{
//passes the actions to its delegate
UIButton *button = (UIButton *)sender;
[cellActionDelegate displaySomeCellRelativePopoverWithInformation:self.info
fromButton:button
fromCell:self];
}
而且,在MyDetailViewController.m:
-(void)displaySomeCellRelativePopoverWithInformation:(MyCellInformationClass *)info
fromButton:(UIButton *)button
fromCell:(UIView *)cell{
UIPopoverController * popoverController = nil;
//create your own UIPopoverController the way you want
//Convert your button/view frame
CGRect buttonFrameInDetailView = [self.view convertRect:button.frame fromView:cell];
//present the popoverController
[popoverController presentPopoverFromRect:buttonFrameInDetailView
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];]
//release objects created...
}
PS:当然,'行动'不必是一个IBAction,该框架从那里的弹出窗口来源不必是一个UIButton-一个单一的)将是:好)
这是我如何做和工作完全正常。
RidersVC *vc = [RidersVC ridersVC];
vc.modalPresentationStyle = UIModalPresentationPopover;
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
UIPopoverPresentationController *popPresenter = [vc popoverPresentationController];
popPresenter.sourceView = vc.view;
popPresenter.barButtonItem= [[UIBarButtonItem alloc] initWithCustomView:button];
popPresenter.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:227.0f/255.0f blue:237.0f/255.0f alpha:1.0];
[self.parentVC presentViewController:vc animated:YES completion:NULL];
这也为我工作:
//Which are the ABSOLUTE coordinates in from the current selected cell CGRect frame =[self.view convertRect:[tbvEventsMatch rectForRowAtIndexPath:indexPath] fromView:tbvEventsMatch.viewForBaselineLayout];
接受的答案现在不能编译。
CGRect rect =[tableView rectForRowAtIndexPath:indexPath]; //This is how to get the correct position on the tableView
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.sourceRect = rect;
popController.permittedArrowDirections = 0; //Here there is no arrow. It is my app's need
popController.delegate = self;
popController.sourceView = self.tableView;
popController.sourceView.backgroundColor = [UIColor yellowColor];