0

5 개의 셀이있는 UIView 클래스 내부에 collectionView를 만들었습니다. TabBar처럼 보이지만 collectionView를 통해 수행하면 내 마음에 맞게 사용자 정의 할 수 있습니다. 이 메뉴 막대는 내 appDelegate 내부에 설정되어 모든보기의 맨 위에 표시됩니다.스위프트 3 : 다른 컨트롤러에 연결되는 UICollectionView로 사용자 정의 TabBar 만들기

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

내가 didSelectItemAt 방법 안에이를 호출하고 indexPath.item가 == 0인지 확인 것, SEGUE : 내가 가진 문제는 내 collectionView의 내부에, 나는 UIView의 내부에이 전화를 할 수없는 오전입니다 첫 번째 컨트롤러에 있지만이 작업을 수행하는 방법에 대한 솔루션을 알아낼 수 없습니다 ... 여기에 내 코드입니다. 어떤 도움이라도 대단히 감사 드리며 대답은 대답으로 표시됩니다. 건배.

앱 위임 :

var window: UIWindow? 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    window = UIWindow(frame: UIScreen.main.bounds) 
    window?.makeKeyAndVisible() 
    window?.rootViewController = UINavigationController(rootViewController: DummyController()) 

    let menuBar: MenuBar = { 
     let mb = MenuBar() 
     return mb 
    }() 

    window?.addSubview(menuBar) 

    _ = menuBar.anchor(nil, left: window?.leftAnchor, bottom: window?.bottomAnchor, right: window?.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 50) 

    return true 
} 

MenuBar 클래스 (사용자 정의 탭 표시 줄) :

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

lazy var collectionView: UICollectionView = { 
    let layout = UICollectionViewFlowLayout() 
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    cv.backgroundColor = .white 
    cv.delegate = self 
    cv.dataSource = self 
    return cv 
}() 

let seperatorView: UIView = { 
    let view = UIView() 
    view.backgroundColor = lightGray 
    return view 
}() 

let imageNames = ["home_selected", "glimpse_selected", "camera_selected", "activity_selected", "profile_selected"] 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    addSubview(collectionView) 
    addSubview(seperatorView) 

    collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "cellId") 

    let selectedIndexPath = IndexPath(item: 0, section: 0) 
    collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .centeredHorizontally) 

    _ = collectionView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0) 

    _ = seperatorView.anchor(collectionView.topAnchor, left: collectionView.leftAnchor, bottom: nil, right: collectionView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 1) 

    setupHorizontalBar() 
} 

var horizontalBarLeftAnchorConstraint: NSLayoutConstraint? 

func setupHorizontalBar() { 
    let horizontalBarView = UIView() 
    horizontalBarView.translatesAutoresizingMaskIntoConstraints = false 
    horizontalBarView.backgroundColor = .darkGray 
    addSubview(horizontalBarView) 

    horizontalBarLeftAnchorConstraint = horizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor) 
    horizontalBarLeftAnchorConstraint?.isActive = true 

    horizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true 

    horizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/5).isActive = true 
    horizontalBarView.heightAnchor.constraint(equalToConstant: 4).isActive = true 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    if indexPath.item == 1 { 
     // segue 
    } 

    let x = CGFloat(indexPath.item) * frame.width/5 
    horizontalBarLeftAnchorConstraint?.constant = x 

    UIView.animate(withDuration: 0.45, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
     self.layoutIfNeeded() 

    }, completion: nil) 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 5 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! MenuCell 

    cell.imageView.image = UIImage(named: imageNames[indexPath.item])?.withRenderingMode(.alwaysTemplate) 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: self.frame.width/5, height: self.frame.height) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 

    return 0 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 
} 
+0

때로는 탭 표시 줄 바꾸기로 사용자 지정보기를 추가했지만 일반적으로 다음과 같이 사용했습니다. ** 1 **. 모든 탭 컨트롤러의 표시 영역이되는 RootViewController를 만듭니다. ** 2 **. 루트 아래에 메뉴 항목이있는 사용자 정의보기를 설정하거나 원하는대로 ** 3 **. 탭 이벤트에 대한 사용자 지정보기와 루트보기 컨트롤러간에 대리인을 설정합니다. ** 4 **. 탭을 받으면 탭 컨트롤러에 대한 자리 표시자가있는 루트보기 컨트롤러의 하위보기로 새보기 컨트롤러를 추가합니다. 필요한 경우 애니메이트 – GoodSp33d

+0

@Jimmy이 코드를 사용해보십시오. UIApplication.shared.keyWindow? .rootViewController? .present (ViewController, animated : true, 완료 : nil) –

답변

0

당신은 UIView의 (또는 다른 비 컨트롤러 클래스) 클래스에서의 UIViewController을 제시 할 수 없다. 하지만 하위 뷰로 추가 할 수 있습니다. UIViewController를 표시하거나 푸시하려면 UIViewController 유형의 클래스에서 수행해야합니다.

menuBar.cellWithIndexSelected = { someIndex in 
    window?.rootViewController = UINavigationController(rootViewController: SomeOtherViewController()) 
} 

다음 didSelectItemAt 방법에 당신이 가진 것

0

당신은 몇 가지 옵션을 가지고, 당신은 다시

var cellWithIndexSelected : ((IndexPath) -> Void)? 

다음 AppDelegate에 당신이 여기에 일부 기능을 첨부 할 수있는 전화를 사용할 수 있습니다 이

if indexPath.item == 1 { 
// segue 
    cellWithIndexSelected(indexPath)  
} 

당신은 NotificationCenter를 사용할 수 있습니다. 또는 proto를 사용할 수 있습니다. col 대리자 패턴.

정상적으로 작동하지만 일반적으로이 탭 표시 줄을 보유하고있는 부모보기 컨트롤러가 있어야하며 앱 대리인 외부에서 이러한 프레젠테이션을 처리해야합니다.