UIButton not visible in UIScrollView with UIPageControl - Swift 3

313 views Asked by At

I am dynamically creating a scrollView and having a page control associated with it.I have an array of buttons and these buttons is being added to the scroll view dynamically.While the buttons thats are added in the first page are visible , some buttons are not visible , but they are being added to the Scroll View.

UIScrollView frame = CGRect(x: 0, y: 0, width: 340, height: 150) scrollView.contentSize = CGSize(width: scrollView.frame.size.width * CGFloat(page), height: scrollView.frame.size.height)

In my case , I have 2 pages , so the content width is 680. On scrolling the view , the content offset of the scroll view is 340 , although the the buttons that are invisible are within the contentSize , the buttons are still not visible ,

Following are the frames of the button that are not visible ,

<UIButton: 0x7ffdf0f26bb0; frame = (355 10; 62 32); opaque = NO; layer = <CALayer: 0x610000030840>>, 
<UIButton: 0x7ffdf0f27110; frame = (427 10; 98 32); opaque = NO; layer = <CALayer: 0x610000030960>>,
<UIButton: 0x7ffdf0f27670; frame = (535 10; 112 32); opaque = NO; layer = <CALayer: 0x610000030a80>>

As far as page control is concerned , even if i remove the page control , I see the same behavior in the scrollview. I am not sure if i have to check for anything specific on the scrollview.

@IBAction func changePage(sender: UIPageControl) -> () {
        let x = CGFloat(pageControl.currentPage + 1) * scrollView.frame.size.width
        scrollView.setContentOffset(CGPoint(x: x,y :scrollView.frame.size.height), animated: true)
    }

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        pageControl.currentPage = Int(pageNumber)
    }
1

There are 1 answers

0
Vinodha Sundaramoorthy On BEST ANSWER

The issues was not with the page control or scrollview , I was using an extension to round corners of the scrollView which was causing this issue ,

extension UIView {

func roundCorners(corners:UIRectCorner, radius: CGFloat) {
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    self.layer.mask = mask
}}

I still don't understand why adding mask to the layer was messing the scrollView.