我注意到在一個項目中,由於某種原因,彈出窗口在右側欄按鈕項目上的位置似乎偏離了右側。我已經嘗試使用UIButton作爲自定義視圖,然後展示該按鈕的彈出窗口,但彈出窗口似乎忽略了showFromRect,如果我實際上提供了「居中」值。UIPopover presentFromBarButton off center
這背後的代碼非常簡單:現在
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareTap:)];
self.navigationItem.rightBarButtonItem = button;
}
- (void)shareTap:(UIBarButtonItem *)button {
self.popover = [[UIPopoverController alloc] initWithContentViewController:[[UIViewController alloc] init]];
[self.popover presentPopoverFromBarButtonItem:button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
,如果我切換到使用內部按鈕,如前所述,我看到類似的行爲(附註顏色變化,使圖像顯示出來)。
該代碼,這仍然是相當簡單:這裏
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *innerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
[innerButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
[innerButton addTarget:self action:@selector(shareTap:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:innerButton];;
}
- (void)shareTap:(UIButton *)button {
self.popover = [[UIPopoverController alloc] initWithContentViewController:[[UIViewController alloc] init]];
// CGRect bottomCenter = CGRectMake(button.frame.size.width/2, button.frame.size.height, 1, 1);
CGRect bottomCenter = CGRectMake(2, button.frame.size.height, 1, 1);
[self.popover presentPopoverFromRect:bottomCenter inView:button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
注意,而不是使用實際的中心,我已經使用了任意值,2。如果我用1或0,彈出窗口變爲左對齊。大於1的任何東西再次對齊。
我搜索了一下週圍尋找一個類似的問題,但我似乎無法找到同樣的問題的人。任何圍繞什麼原因或如何避免它的想法將不勝感激。我唯一能夠猜到的是,由於它靠近邊緣,蘋果會做一些定位巫術來迫使它成爲他們想要的地方。問題是右上角的按鈕看起來像是一個非常標準的下拉位置。
編輯:我已經證實,在本地郵件應用程序中,長按「新郵件」圖標也會發生同樣的行爲。
編輯2:我能夠向Apple確認這是一個問題。在其中一個更新的版本中(我記不清哪一個是我認爲的9箇中的一個),他們這樣做是爲了您可以手動設置它。我相信默認行爲仍然是錯誤的(我還沒有嘗試過這一段時間),但是如果你這麼傾向,你可以使用CGRect偏移方法使它正常工作。
嘗試此 的UIBarButtonItem *按鈕= [[ALLOC的UIBarButtonItem] initWithBarButtonSystemItem:UIBarButtonSystemItemAction目標:自動作:@selector(輕觸分享:)]; – Nishant
你能解釋一下嗎?使用系統項目本身並不能修復按鈕的位置,在我的編輯中我提到了默認的應用程序也展示了這個問題(郵件),這大概是使用系統圖標。 – shortstuffsushi