自定义标签栏菜单,水平滑动

时间:2018-05-19 17:24:36

标签: swift xcode

关于实施的一个简单问题。

我正在尝试重新创建:

https://i.imgur.com/lFXKFXl.mp4

我想知道我是否必须使用UICollectionView从头开始,有一个内置的Xcode方法,或者某个地方有一个库。

注意:我不打算在底部创建标签栏菜单,而是在导航控制器导航栏下方的顶部创建水平滑动功能。 Instagram也在搜索标签下面有这个。

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用-1, Microsoft.SharePoint.Client.InvalidClientQueryException The expression "'CountryCDH:("USA")','(ColorChoiceCDH:("Red"),ColorChoiceCDH:("Blue"))'" is not valid. 并向其添加子视图控制器。我对UIViewController进行了扩展,可以轻松地创建滑动视图。

UIScrollView

对于标签栏菜单,您可以通过在extension UIViewController { public func addViewControllerToContainer(viewController: UIViewController, count: CGFloat = 0, container: UIScrollView, heightDecrease: CGFloat = 0, startIndex: CGFloat = 0) { let multiplier: CGFloat = count addChildViewController(viewController) container.addSubview(viewController.view) container.contentSize.width += viewController.view.frame.width container.contentSize.height = viewController.view.frame.height - heightDecrease container.frame = CGRect(x: viewController.view.frame.origin.x, y: viewController.view.frame.origin.y, width: viewController.view.frame.width, height: viewController.view.frame.height - heightDecrease) container.frame.origin.y += heightDecrease container.contentOffset.x = container.frame.width * startIndex viewController.view.frame.size.width = container.frame.width viewController.view.frame.size.height = container.frame.height viewController.view.frame.size.height -= heightDecrease viewController.view.frame.origin.x = container.frame.width * multiplier } public func addViewControllersToContainer(viewControllers: [UIViewController], container: UIScrollView, heightDecrease: CGFloat = 0, startIndex: CGFloat) { var count: CGFloat = 0 for viewController in viewControllers { addViewControllerToContainer(viewController: viewController, count: count, container: container, heightDecrease: heightDecrease, startIndex: startIndex) count += 1 } } } class MainSwipeViewController: UIViewController { let viewControllers: [UIViewController] = [VC1, VC2, VC3] // view controllers swiped between let startIndex: CGFloat = 0 // which viewController to begin at (0 means first) let mainScrollView: UIScrollView = { let scrollView = UIScrollView() scrollView.backgroundColor = .lightGray scrollView.isPagingEnabled = true scrollView.showsHorizontalScrollIndicator = false scrollView.bounces = false scrollView.contentInsetAdjustmentBehavior = .never scrollView.backgroundColor = UIColor.clear return scrollView }() private func addViewControllers(_ vc: UIViewController, viewControllers: [UIViewController], startIndex: CGFloat = 0) { view.addSubview(mainScrollView) addViewControllersToContainer(viewControllers: viewControllers, container: mainScrollView, startIndex: startIndex) } override func viewDidLoad() { super.viewDidLoad() addViewControllers(self, viewControllers: viewControllers, startIndex: startIndex) } } 中实施UIScrollViewDelegate轻松创建

MainSwipeViewController

extension MainSwipeViewController: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { let contentOffset = scrollView.contentOffset.x / view.frame.width } } 是您滑动的间隔,每次滑动时都会增加1。这将帮助您为菜单中的蓝色条设置动画。

答案 1 :(得分:1)

您可以按照以下方式为蓝色条设置动画

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
  blueBarViewLeadingConstraint.constant = scrollView.contentOffset.x / (scrollView.bounds.width / (TabBarView.frame.size.width / numberOfTab)) 
}