Customise Position of UIPageControl

雨燕双飞 提交于 2019-12-01 09:29:45
Hussein Dimessi

here s a very neat and 100% effective way to get to change the position of the pageControl

extension UIPageViewController {
  override open func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()

      for subV in self.view.subviews {
          if type(of: subV).description() == "UIPageControl" {
              let pos = CGPoint(x: newX, y: newY)
              subV.frame = CGRect(origin: pos, size: subV.frame.size)
          }
      }
  }
}

The project uses a UIPageViewController to handle the display and movement through the content. You can supply data to that object so it displays a UIPageControl as you say. BUT you have no control over the display of that item, other than maybe some colour styling. If you want to position it, you'll need to implement you're own instance of UIPageControl and handle it's content, position and changes manually.

Override the viewDidLayoutSubviews() of the pageviewcontroller and use this

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // get pageControl and scroll view from view's subviews
    let pageControl = view.subviews.filter{ $0 is UIPageControl }.first! as! UIPageControl
    let scrollView = view.subviews.filter{ $0 is UIScrollView }.first! as! UIScrollView
    // remove all constraint from view that are tied to pagecontrol
    let const = view.constraints.filter { $0.firstItem as? NSObject == pageControl || $0.secondItem as? NSObject == pageControl }
    view.removeConstraints(const)

    // customize pagecontroll
    pageControl.translatesAutoresizingMaskIntoConstraints = false
    pageControl.addConstraint(pageControl.heightAnchor.constraintEqualToConstant(35))
    pageControl.backgroundColor = view.backgroundColor

    // create constraints for pagecontrol
    let leading = pageControl.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor)
    let trailing = pageControl.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor)
    let bottom = pageControl.bottomAnchor.constraintEqualToAnchor(scrollView.topAnchor, constant:8) // add to scrollview not view

    // pagecontrol constraint to view
    view.addConstraints([leading, trailing, bottom])
    view.bounds.origin.y -= pageControl.bounds.maxY
}

is your page control encompassed within some other View, if so then you may be setting the co-ordinates wrong, try to put log of your page control.frame so as to know where it lies

If using storyboards, place a UIPageControl object using the menu in the bottom right and set constraints.

If using frames, just programmatically add it:

var pageControl = UIPageControl()
pageControl.frame = CGRectMake(0,0,0,0) <- These are the coordinates.
self.view.addSubView(pageControl)

if you set the frame for UIPageControl , it doesnot work. Other than this you can set the transform.

[_pageControl setTransform:CGAffineTransformMakeTranslation(100, 0.0)];

enjoy coding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!