2017-09-06 9 views
2

보기가 실제보기 위로 넘어갈 수 있도록보기 도구 모음에서보기를 모달로 표시하려면 어떻게합니까?탭 모음 컨트롤러에서 모달로보기보기

카메라로 뷰를 만들고 싶습니다. 가운데에 사용자가 클릭하고 카메라보기가 표시되는 단추가있는 "WhatsApp"또는 "Instagram"과 같은 것입니다.

또한 사용자는 닫기 버튼을 클릭 할 때 이전 탭을 이동해야합니다.

This is how my ViewController is connected to the TabBarController

답변

3

내가 현재 짓고 있어요 응용 프로그램에서 비슷한 구현 했어, 그것은 상대적이다 직접 수행하려면이 작업을 수행하기 위해 UITabBarController의 대리자 메서드를 구현해야합니다.

구현해야 위임 방법은 다음과 같습니다 당신의 탭을 선택에서 탭 컨트롤러를 중지합니다이 메소드로부터 false를 반환 tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool

, 당신은 단지 프로그래밍 방식 UIViewController을 제시하기 위해 자신의 논리를 구현해야합니다. 내 구현은 약간 다릅니다 더 많은 변수가 나는 위의 코드를 테스트하지했습니다

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { 

    // If your view controller is emedded in a UINavigationController you will need to check if it's a UINavigationController and check that the root view controller is your desired controller (or subclass the navigation controller) 
    if viewController is YourViewControllerClass { 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     if let controller = storyboard.instantiateViewController(withIdentifier: "storyboardID") as? YourViewControllerClass { 
      controller.modalPresentationStyle = .fullScreen 
      self.present(controller, animated: true, completion: nil) 
     } 

     return false 
    } 

    // Tells the tab bar to select other view controller as normal 
    return true 
} 

:

다음은 예입니다. 일반적인 원칙은 동일합니다.

어떻게 지내는지 알려주십시오. 필요한 경우 답변을 업데이트하겠습니다.

+0

나는 당신의 코드를 사용했고 그것이 작동하기를 원하는 방식으로 작동하지 않습니다. 그래서 내 TabBarController에 대한 새로운 클래스를 만들고 거기에 코드를 붙여 넣습니다. 그게 무슨 뜻인가요? YourViewControllerClass 대신 어떤 클래스를 써야합니까? 어쩌면 당신은 나의 원래 포스트에서 연결 한 나의 그림을 볼 수있다. –

+0

이봐, 당신은'UITabBarController'를 서브 클래스 화하고이 클래스에'UITabBarDelegate'를 구현해야합니다. 'viewDidLoad'에서 당신은'self.delegate = self'를 할 필요가 있습니다. 그러면 아래쪽에있는 탭을 선택할 때 위의 메소드가 실행됩니다. 'YourViewControllerClass'는 스토리 보드의 뷰 컨트롤러 (왼쪽에있는 뷰 컨트롤러)와 연관된 클래스입니다. 'UIViewController'를 서브 클래스 화하고이 클래스를 해당 뷰 컨트롤러에 적용해야합니다. – WsCandy

+0

Yeey! 그것은 작동합니다. 도와 주셔서 정말로 고맙습니다. self.delegate = self는 잊어 버렸습니다.이제 버튼으로 닫으 려한다면 어떻게하면 ViewController로 돌아갈 수 있습니까? –

0
let modalVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifier") 
    modalVC.modalTransitionStyle = .crossDissolve 
    modalVC.modalPresentationStyle = .full or .overfullscreen // please check which of the options work 
    self.present(modalVC, animated: true, completion: { 

    }) 
+0

이 코드 조각을 어디에 넣어야합니까? –

0

당신이 UITabBarControllerDelegate에 부합되는 것을 가정하면 구현할 수 :

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { 

    // here, you should edit "0" to be matched with your selected item 
    // for instance, if there is 5 items and the desired item is in the middle, the compared value should be "2" 
    if tabBarController.selectedIndex == 0 { 

     // simply, you will need to get the desired view controller and persent it: 
     let desiredStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     let desiredViewController = desiredStoryboard.instantiateViewController(withIdentifier: "storyboard id") 

     present(desiredViewController, animated: true, completion: nil) 

    } 
} 
+0

스토리 보드에서 직접 segue 관계를 만들었으므로 탭 표시 줄 항목이 자동으로 만들어집니다. 그래서 저는 이미 탭 바 컨트롤러에 연결된 뷰를 가지고 있지만, 다른 뷰로 전환하고 싶지 않기 때문에 사용하고 싶지 않습니다. 나는 그것을 팝업 싶습니다. 어떻게해야합니까? –

+0

자세히 설명해 주시겠습니까? –

+0

가서 내 원래 게시물을 확인하십시오. 내 문제는보기를 모달로 표현하는 대신 TabBarController 때문에보기로 전환한다는 것입니다. 그리고 이렇게 구현했기 때문에 그렇습니다.하지만 지금은보기를 전환하지 않고 모달로 표시하는 방법을 묻습니다. 그래서 그것은 나의 실제적인 견해 위에 나타납니다. –