改变页面控件的位置
Posted
技术标签:
【中文标题】改变页面控件的位置【英文标题】:to change the position of pagecontrol 【发布时间】:2018-04-24 04:59:16 【问题描述】:我在UIPageViewController
中使用了UIPageContol
,如下所示:-
uipageviewcontroller 类:-
class PageControllerViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate
private var pageViewModel :PageViewModel!
init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]? = nil, withViewModel viewModel:PageViewModel)
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: options)
pageViewModel = viewModel
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
let pageControl = UIPageControl()
override func viewDidLoad()
super.viewDidLoad()
self.dataSource = self
self.delegate = self
createPageViewController()
setupPageControl()
fileprivate func createPageViewController()
if (pageViewModel.datasourceModel.dataListArray?.count)! > 0
let firstController = getItemController(0)!
let startingViewControllers = [firstController]
self.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
fileprivate func setupPageControl()
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.red
appearance.currentPageIndicatorTintColor = UIColor.black
appearance.backgroundColor = UIColor(red: 0.93, green: 0.86, blue: 1, alpha:1.0)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
let itemController = viewController as! HomeViewController
if itemController.itemIndex > 0
return getItemController(itemController.itemIndex-1)
return nil
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
let itemController = viewController as! HomeViewController
if itemController.itemIndex+1 < (pageViewModel.datasourceModel.dataListArray?.count)!
return getItemController(itemController.itemIndex+1)
return nil
fileprivate func getItemController(_ itemIndex: Int) -> HomeViewController?
if itemIndex < (pageViewModel.datasourceModel.dataListArray?.count)!
let homeViewModel :HomeViewModel = QM_HomeViewModel(withmodel:pageViewModel.datafordisplay(objectatindex: itemIndex))
let ViewController = HomeViewController(nibName: "HomeViewController", bundle: nil, withViewModel: homeViewModel)
ViewController.itemIndex = itemIndex
return ViewController
return nil
// MARK: - Page Indicator
func presentationCount(for pageViewController: UIPageViewController) -> Int
return pageViewModel.datasourceModel.dataListArray! .count
func presentationIndex(for pageViewController: UIPageViewController) -> Int
return 0
// MARK: - Additions
func currentControllerIndex() -> Int
let pageItemController = self.currentController()
if let controller = pageItemController as? HomeViewController
return controller.itemIndex
return -1
func currentController() -> UIViewController?
if (self.viewControllers?.count)! > 0
return self.viewControllers![0]
return nil
HomeViewController:-
var itemIndex: Int = 0
@IBOutlet weak var restaurantname: UILabel!
@IBOutlet weak var offertype: UILabel!
@IBOutlet weak var aboutoffer: UILabel!
@IBOutlet weak var price: UILabel!
@IBOutlet weak var desc: UILabel!
@IBOutlet weak var dealtype: UILabel!
private var homeViewModel :QM_HomeViewModel!
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, withViewModel viewModel:QM_HomeViewModel)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
homeViewModel = viewModel
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
override func viewDidLoad()
super.viewDidLoad()
self.restaurantname.text = homeViewModel.homeModel.restaurtantname
self.offertype.text = homeViewModel.homeModel.offertype
self.aboutoffer.text = homeViewModel.homeModel.aboutoffer
self.desc.text = homeViewModel.homeModel.desc
self.price.text = homeViewModel.homeModel.price
self.dealtype.text = homeViewModel.homeModel.dealtype
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
This is the output that i got .Here it works the pageviewcontroller.
when i give as self.tabBarController?.tabBar.isHidden = true in Homeviewcontroller.I got the output as shown.
所以我需要这个标签栏上方的页面控件。如何做
【问题讨论】:
如何设置pagecontrol的位置 查看更新后的答案,如果您发现任何问题,请告诉我 @Dhiru 你能帮我解决这个问题吗 【参考方案1】:改变位置
let pageControlWidht = 200 // depending number of dots
let pageControl : UIPageControl = UIPageControl.init(frame:CGRect.init(x: self.view.frame.size.with/2-pageControlWidht, y: self.view.frame.size.hieght-100, width: pageControlWidht, height: 100))
为了将UIPageControl
带到上面。你可以使用
self.view.bringSubview(toFront: self.pageControl)
您可以使用以下代码跟踪当前页面,其中PageContentVC
是自定义ViewController
在PageViewController
内滚动,您需要在每个ViewController
中存储索引
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
if !completed
let pageContentView = previousViewControllers[0] as! PageContentVC;
self.pageControl.currentPage = pageContentView.pageIndex;
【讨论】:
对不起,这不是我需要的。我需要设置页面控件的位置 能否请您添加截图,并说明您的要求 pagecontectVc 是什么...我需要创建其他视图控制器吗 基本思想是将索引存储在UIViewController
的每个实例中,这里PageContentVC
是我的自定义viewController在我的UIPageViewController
中滚动。你可以使用自己的类
appcoda.com/uipageviewcontroller-tutorial-intro 这是示例演示。在底部,我使用高度为 49 的标签栏。我需要标签栏控制器上方的 uipagecontrol。以上是关于改变页面控件的位置的主要内容,如果未能解决你的问题,请参考以下文章