我想要2个菜单(另一个菜单与截图中显示的菜单相同,但它包含其他项目。)
每个菜单都有自己的UIStackView
。
我决定排除UIPageViewController
,而是使用UIScrollView
,因为这是我想要的行为。
如何将两个菜单放在UIScrollView
内并制作精彩的动画?
答案 0 :(得分:1)
您是否因为复杂性而避免使用UIPageViewController?因为如果你有一个更简单的方法来使用UIScrollView的pagingEnabled属性完成相同的手势控制转换
根据我在截图中看到的内容......
var size = CGSize()
size.height = self.view.frame.height
size.width = self.view.frame.width * 2
let scrollView = UIScrollView()
scrollView.frame = self.view.frame
scrollView.contentSize = size
scrollView.pagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
nav.center.y = scrollView.frame.height / 2
nav.center.x = scrollView.frame.width / 2
nav2.center.y = scrollView.frame.height / 2
nav2.center.x = nav.center.x + scrollView.frame.width
scrollView.addSubview(nav1)
scrollView.addSubview(nav2)
self.view.addSubview(scrollView)
答案 1 :(得分:0)
@dsieczko回答我的问题做得很棒。我对AutoLayout没有经验,所以这就是问题所在。我的最终代码看起来像这样:
import UIKit
class MainMenuViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var nav2: UIStackView!
@IBOutlet var nav: UIStackView!
@IBOutlet var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
/* MAIN MENUS */
var size = CGSize()
size.height = self.nav.frame.height
size.width = self.view.frame.width * 2
scrollView.frame = self.view.frame
scrollView.contentSize = size
scrollView.pagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
nav.center.y = scrollView.frame.height / 2
nav.center.x = self.view.frame.width * 1.5
nav2.center.y = scrollView.frame.height / 2
nav2.center.x = nav.center.x + scrollView.frame.width + 400
scrollView.addSubview(nav)
scrollView.addSubview(nav2)
self.view.addSubview(scrollView)
/* MAIN MENUS */
// Change the Navigation Bar Color to Red
navigationController!.navigationBar.barTintColor = UIColor(red: 0.79, green: 0.19, blue: 0.19, alpha: 1)
// Change the Navigation Bar Font to Lato
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "Lato-Semibold", size: 20)!]
}
}
我的布局类似于this:
如果有人遇到这样的问题,我很乐意帮助解决这个问题。