2017-04-06 12 views
1

Ich versuche, mehrere Schaltflächen in einer ScrollView, aber es scrollt nur Hintergrundansicht.Wie Sie mehrere Schaltflächen in einer Bildlaufleiste programmgesteuert hinzufügen

class HomeVC: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     navigationItem.title = "Home" 

     let scrollView = UIScrollView() 

     let view = UIView() 
     scrollView.frame = self.view.bounds 
     self.view.backgroundColor = .green 
     scrollView.backgroundColor = .blue 
     scrollView.addSubview(view) 
     self.view.addSubview(scrollView) 
     scrollView.isPagingEnabled = true 
     scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 3) 
     view.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height) 
     view.backgroundColor = .yellow 

     attractivePlaceButtonSetup() 
     eatDrinkButtonSetup() 
     ShoppingButtonSetup() 
     festivalEventButtonSetup() 
     hotelGuestHouseButtonSetup() 
     travellerEssentialButtonSetup() 
     dealButtonSetup() 
     seeDoButtonSetup() 
    } 

Ich schrieb diesen Code für Schaltfläche Rahmen func eatDrinkButtonSetup() {

 let button = UIButton() 
     button.frame = CGRect(x: 5, y: 225, width: self.view.frame.size.width - 10, height: 150) 
     button.setTitle("Eat & Drink", for: .normal) 
     button.setBackgroundImage(#imageLiteral(resourceName: "imageName"), for: .normal) 
     button.titleEdgeInsets = UIEdgeInsets(top: -120, left: -200, bottom: 0, right: 0) 
      button.addTarget(self, action: #selector(targetEatDrink), for: .touchUpInside) 

     view.addSubview(button) 

    } 

} 

Ich versuche auch, auf eine solche Art und Weise, aber es scrollen Sie einfach auf einen Knopf.

scrollView.addSubview(attractivePlaceButtonSetup) 
self.view.addSubview(scrollView) 
+0

Warum setzen Sie Ansichtsrahmen y self.view.frame.size.height? in Zeile view.frame = CGRect (x: 0, y: self.view.frame.size.height, Breite: self.view.frame.size.width, height: self.view.frame.size.height) –

+0

@JasmeetKaur Kaur Entschuldigung, ich habe keine Ahnung –

+0

Was machen Ihre ButtonSetup() Funktionen? Kannst du dem Code auch mindestens einen von ihnen zeigen? – DonMag

Antwort

1
//Try this... 
    //Background Scroll Creation 

      var stickersScrollViewCount = 0 
      func stickersScrollContents() { 

       var xCoord: CGFloat = 5 
       let yCoord: CGFloat = 5 
       let buttonWidth:CGFloat = 45.0 
       let buttonHeight: CGFloat = 45.0 
       let gapBetweenButtons: CGFloat = 5 

       for i in 0..<stickersImageArray.count{ 
        stickersScrollViewCount = i 
        // Button properties 
        let filterButton = UIButton(type: .custom) 
        filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight) 
        filterButton.tag = stickersScrollViewCount 
        filterButton.backgroundColor = UIColor.clear 
        filterButton.setTitleColor(UIColor.white, for: .normal) 
        filterButton.titleLabel?.adjustsFontSizeToFitWidth = true 
        filterButton.showsTouchWhenHighlighted = true 
        let myimage = UIImage(named: stickersImageArray[stickersScrollViewCount]) 
        filterButton.setImage(myimage, for: .normal) 
        filterButton.addTarget(self, action:#selector(StickersActionTapped), for: .touchUpInside) 
        filterButton.layer.cornerRadius = 5 
        filterButton.clipsToBounds = true 
        xCoord += buttonWidth + gapBetweenButtons 
        bgScrollView.addSubview(filterButton) 

       } 
       bgScrollView.contentSize = CGSize(width: buttonWidth * CGFloat(stickersScrollViewCount+2), height: yCoord) 

      } 

//Call the function where ever you want viewDidLoad() or on Button Click!! 

//Hope this helps!!! 
Verwandte Themen