Scrollview with horizontal image display with more button iOS swift



We had one scrollView.In that scrollView, we will show the dynamic images.If the user clicks on that image we will execute one action.If the images are more not to show in the scrollView within the view not the scroll then we have to put one more button on the view.We will create one global value as xPos.We will save the next x value on that variable.Every image is added then we will increase the x value.
Code:
                for (index, element) in array.enumerated() {

                    var newImage = UIButton(type: .custom)
                    newImage = UIButton(frame: CGRect(x: xPos, y: 0, width: 100, height: 100))
                    newImage.addTarget(self, action: #selector(self.openView(sender:)), for: .touchUpInside)
                    if (xPos-100)<(scroll.frame.size.width) {
                        newImage.setImage(name:"imagename.png"!), for: .normal)
                    } else {
                        newImage.setTitle("More", for: .normal)
                        newImage.backgroundColor = UIColor.black
                        scroll.addSubview(newImage)
                        scroll.contentSize.width = xPos+100
                        xPos = newImage.frame.origin.x + newImage.frame.size.width
                        newImage.addTarget(self, action: #selector(self.openView(sender:)), for: .touchUpInside)
                        break
                    }
                    scroll.addSubview(newImage)
                    xPos = newImage.frame.origin.x + newImage.frame.size.width + 2
                    newImage.imageView?.contentMode = UIViewContentMode.scaleAspectFill
}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift