簡體   English   中英

手動設置 UIPopover 箭頭方向和位置

[英]Set UIPopover arrow direction and position manually

我正在使用 UIBarButtonItem。 在我的場景中,當我雙擊該欄按鈕時,我想從每個欄按鈕顯示一個彈出框。 所以我使用了 UIBarButtonItem 中的 UITapGesture。 但是,彈出箭頭總是出現在所有 UIBarButtonItem 的中間。

我無法讓箭頭出現在每個條形按鈕的中間。 我的朋友告訴我使用點設置箭頭方向,但我不知道該怎么做。

如何設置彈出框箭頭的位置和方向?

使用以下命令設置彈出框的方向:

[yourPopover setPopoverArrowDirection: UIPopoverArrowDirectionDown];

您還可以使用UIPopoverArrowDirectionUpUIPopoverArrowDirectionLeftUIPopoverArrowDirectionRightUIPopoverArrowDirectionAny

對於斯威夫特

yourPopover?.permittedArrowDirections = .up // or .down, .left, .right

我使用方法allowedArrowDirections = .Down ,它對我有用。 快速示例:

if let popoverPresentationController = shareViewController.popoverPresentationController {
    popoverPresentationController.permittedArrowDirections = .Down
    popoverPresentationController.delegate = self
    popoverPresentationController.sourceView = sender as! UIButton
    popoverPresentationController.sourceRect = CGRectMake(0, 0, sender.frame.size.width, sender.frame.size.height)
}

如果您使用的是 IBAction 方法,則可以使用(id)sender作為其參數。 (即調用按鈕) 然后您可以將彈出框的附件矩形設置為按鈕的框架。 箭頭將盡最大努力沿着該框架的側面固定在適當的位置。 請參閱UIPopoverController 類參考

-(IBAction)sortButtonPressed:(id)sender {
  // Set up your popover class, this varies per your implementation
  MyPopoverClass *iPop = [[MyPopoverClass alloc] initWithNibName:@"MyPopover" bundle:nil];
  UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:iPop];

  // Grab the button info from the input parameter
  UIButton *button = (UIButton *)sender;
  CGRect buttonRectangle = button.frame;

  // Set the arrow direction
  UIPopoverDirection dir = UIPopoverArrowDirectionAny; // Or Up, Down, etc.

  // Put it all together
  [popover presentPopoverFromRect:buttonRectangle inView:mySubView permittedArrowDirections:dir animated:YES];

  [popover release]
  [iPop release];
}

如有必要,您還可以手動對該 CGRect 進行硬編碼。 縮小矩形的寬度/高度將使您能夠更嚴格地控​​制箭頭的位置。

您可以在 Storyboard Segue 的屬性檢查器中設置箭頭的方向。 例如,我僅在以下屏幕截圖中將箭頭方向設置為向下: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM