[英]Is it Possible to add a custom Navigation Bar for CNContactViewController?
[英]CNContactViewController hiding navigation bar
當我將CNContactViewController
推CNContactViewController
UINavigationController
內的UITableViewController
子類的堆棧時,頂部導航欄幾乎完全隱藏。 但是隨着亮度一直向上,你會看到后面的箭頭,然后是“細節”和系統狀態欄。 當我點擊屏幕的那個角落時,CNContactViewController確實被解雇了。
當然這不好,因為用戶可能甚至看不到導航欄的文本,現在按任何按鈕來解除。
有沒有辦法讓CNContactViewController的導航欄色調與顯示它的視圖控制器(我的應用程序的其余部分)相同?
CNContactViewController *controller = [CNContactViewController viewControllerForUnknownContact:person];
controller.contactStore = [[CNContactStore alloc] init];
controller.delegate = self;
controller.allowsActions = NO;
[self.navigationController pushViewController:controller animated:YES];
我應該注意到我只是在iOS 10上遇到這個問題,而不是10以下的版本。當我點擊“添加到現有聯系人”時,我也得到了正確着色的導航欄,但是當該視圖控制器被解除時它再次中斷。
所以再一次,我的問題是: 有沒有辦法讓CNContactViewController的導航欄色調與顯示它的視圖控制器(我的應用程序的其余部分)相同?
您的第二個屏幕截圖顯示了此問題的原因:您已將條形(或一般條形按鈕項目)的色調顏色設置為白色。 因此,它們在透明導航條前面是白色的,在接觸視圖控制器中是白色背景。
您無法直接對條形色調進行任何操作,但您可以通過以下兩種方式之一解決此問題:
一個是讓你的導航欄不透明。 在這種情況下,聯系人視圖控制器的導航欄將為黑色,並且您的白色條按鈕項將可見。
另一種方法是在聯系人視圖控制器按下時更改導航欄的色調顏色(不是條紋色調,而是它向下傳遞到其按鈕項目的色調顏色),並在彈出時更改它。
編輯好的,我看到還有一個問題,因為New Contact視圖控制器是在你面前呈現的另一個視圖控制器。 如果您拒絕放棄白條按鈕項目設置,則在推動聯系人視圖控制器時,必須使用外觀代理將UIBarButtonItem色調設置為其他顏色,然后在導航控制器時將其重置為白色delegate告訴您用戶正在彈回您的視圖控制器。
我花了幾個小時與CNContactViewController進行摔跤,試圖強制它適合我的UINavigation外觀設置,但它不會。 它有自己的外觀和感覺。 我看了一下iOS應用程序,比如Mail和Notes,看看它們是如何顯示CNContactViewController的,它似乎顯示為一個popover,所以我也是這樣。
即使這不是微不足道的。 CNContactViewController必須包裝在UINavigationView中,以便Create New Contact和其他視圖可以推送。 如果您已覆蓋UINavigationBar外觀默認值,則需要在之前和之后將它們設置回標准。 以下是它最終的表現:
@property (strong, nonatomic) CNContactViewController *contactViewController;
@property (assign, nonatomic) UIBarStyle saveAppearanceBarStyle;
@property (strong, nonatomic) UIColor *saveAppearanceBarTintColor;
@property (strong, nonatomic) UIColor *saveAppearanceTintColor;
@property (strong, nonatomic) UIColor *saveAppearanceBackgroundColor;
@property (strong, nonatomic) NSDictionary<NSAttributedStringKey, id> * saveAppearanceTitleTextAttributes;
@property (assign, nonatomic) BOOL saveAppearanceTranslucent;
- (IBAction)onAddToContactsButtonTapped:(id)sender
self.contactViewController = [CNContactViewController viewControllerForUnknownContact: ... ]; // as before
[self suspendNavBarAppearanceSettings];
UINavigationController *popNav = [[UINavigationController alloc] initWithRootViewController:self.contactViewController];
popNav.modalPresentationStyle = UIModalPresentationPopover;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onAddToContactsDoneTapped:)];
self.contactViewController.navigationItem.leftBarButtonItem = doneButton;
[self presentViewController:popNav animated:YES completion:nil];
UIPopoverPresentationController *popoverController = newNav.popoverPresentationController;
popoverController.permittedArrowDirections = UIPopoverArrowDirectionAny;
popoverController.sourceRect = ...; // where you want it to point
popoverController.sourceView = ...; // where you want it to point
popoverController.delegate = self;
}
- (void) suspendNavBarAppearanceSettings
{
self.saveAppearanceBarStyle = [UINavigationBar appearance].barStyle;
self.saveAppearanceBarTintColor = [UINavigationBar appearance].barTintColor;
self.saveAppearanceTintColor = [UINavigationBar appearance].tintColor;
self.saveAppearanceBackgroundColor = [UINavigationBar appearance].backgroundColor;
self.saveAppearanceTitleTextAttributes = [UINavigationBar appearance].titleTextAttributes;
self.saveAppearanceTranslucent = [UINavigationBar appearance].translucent;
[UINavigationBar appearance].barStyle = UIBarStyleDefault;
[UINavigationBar appearance].barTintColor = nil;
[UINavigationBar appearance].tintColor = nil;
[UINavigationBar appearance].backgroundColor = nil;
[UINavigationBar appearance].titleTextAttributes = nil;
[UINavigationBar appearance].translucent = YES;
}
- (void) restoreNavBarAppearanceSettings
{
[UINavigationBar appearance].barStyle = self.saveAppearanceBarStyle;
[UINavigationBar appearance].barTintColor = self.saveAppearanceBarTintColor;
[UINavigationBar appearance].tintColor = self.saveAppearanceTintColor;
[UINavigationBar appearance].backgroundColor = self.saveAppearanceBackgroundColor;
[UINavigationBar appearance].titleTextAttributes = self.saveAppearanceTitleTextAttributes;
[UINavigationBar appearance].translucent = self.saveAppearanceTranslucent;
}
- (void)onAddToContactsDoneTapped:(id)sender
{
[self restoreNavBarAppearanceSettings];
[[self presentedViewController] dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - CNContactViewControllerDelegate
- (void)contactViewController:(CNContactViewController *)viewController
didCompleteWithContact:(nullable CNContact *)contact
{
[self restoreNavBarAppearanceSettings];
[[self presentedViewController] dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIPopoverPresentationControllerDelegate
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
// This method is only called if we are presented in a popover, i.e. on iPad
// as opposed to full screen like on a phone.
// on iPad you just tap outside to finish, so remove the Done button
self.contactViewController.navigationItem.leftBarButtonItem = nil;
}
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
[self restoreNavBarAppearanceSettings];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.