2010-12-08 33 views
5

我正在使用UIBarButtonItem。在我的場景中,當我雙擊該欄按鈕時,我想顯示來自每個欄按鈕的彈出窗口。所以我使用該UIBarButtonItem的UITapGesture。但是,彈出箭頭總是出現在所有UIBarButtonItem的中間。手動設置UIPopover箭頭的方向和位置

我不能讓箭頭出現在每個欄按鈕的中間。我的朋友告訴我用點設置箭頭方向,但我不知道該怎麼做。

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

+0

owhh..thank的...我從來沒有意識到這一點之前:) – 2010-12-08 06:06:14

回答

8

設置使用酥料餅的方向:

[yourPopover setPopoverArrowDirection: UIPopoverArrowDirectionDown]; 

您還可以使用UIPopoverArrowDirectionUpUIPopoverArrowDirectionLeftUIPopoverArrowDirectionRightUIPopoverArrowDirectionAny

+0

我知道這個方向發展,但我的意思是我想改變方向的位置..我使用UIPopoverArrowDirectionUp,但我想箭頭的位置是在X = 50和Y = 100,我可以這樣做嗎? – 2010-12-08 06:05:45

+0

幾個月前,我真的沉迷於此......不幸的是,您無法指定箭頭的位置,只能顯示uiipopover提供的矩形。 – 2010-12-08 06:06:37

0

如果您使用IBAction方法,則可以使用(id)sender作爲其參數。 (即調用按鈕)然後,您可以將彈出窗口的附件矩形設置爲按鈕的框架。箭頭將盡最大努力在適當的位置沿着該框架的側面附着。見UIPopoverController Class Reference

-(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進行硬編碼。縮小矩形的寬度/高度將使您能夠更緊密地控制箭頭的位置。

12

我使用的方法allowedArrowDirections = .Down,它適用於我。 Swift示例:

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) 
} 
0

您可以在Storyboard Segue的屬性檢查器中設置箭頭的方向。例如,我只在下面的截圖設置箭頭方向向下: enter image description here