0

i've created a paged UIScrollView which contains labels on every page. How can i lower the margin between the labels like in the picture and then still being able to scroll?

At the moment my navigationBar look like following. Where u can swipe to next page to the right and get the next label.

enter image description here

What i want is something like this with a small margin and still being able to swipe.

enter image description here

viewDidLoad code

    //Category ScrollView
    categoryScrollView = UIScrollView(frame: CGRectMake(0, self.navigationController!.navigationBar.frame.height-38, self.view.frame.width, 38))
    categoryScrollView?.contentSize = CGSizeMake(self.view.frame.width, 38)
    categoryScrollView?.delegate = self
    categoryScrollView?.pagingEnabled = true
    self.navigationController?.navigationBar.addSubview(categoryScrollView!)
    
    categoryArray = NSArray(objects: "Book", "Elektronik")
    
    var textWidth = 0
    
    for val in categoryArray!
    {
        var textLabel: UILabel = UILabel()
        textLabel.textAlignment = NSTextAlignment.Center
        textLabel.text = val as NSString
        textLabel.frame = CGRectMake(CGFloat(textWidth), 0, categoryScrollView!.frame.width, categoryScrollView!.frame.height)
            
            
        categoryScrollView?.addSubview(textLabel)
        
        textWidth = textWidth + Int(textLabel.frame.size.width)
        
        if textWidth > Int(self.view.frame.width) {
            categoryScrollView?.contentSize = CGSizeMake(CGFloat(textWidth), categoryScrollView!.frame.height);
            
        }
        
        
    }
Community
  • 1
  • 1
Peter Pik
  • 11,023
  • 19
  • 84
  • 142

1 Answers1

0

The default width of the "page" when pagingEnabled is switched on is the width of the scroll view. You could reduce the width and let the contained views be visible beyond the edges of the view by setting clipsToBounds to false.

If this does not work for you there also the option to adjust the paging width by intercepting the end of the drag via the UIScrollViewDelegate method scrollViewWillEndDragging(...) where you can adjust the targetContentOffset to fit your needs (see an example here).

Community
  • 1
  • 1
Mundi
  • 79,884
  • 17
  • 117
  • 140