2017-04-06 1 views
1

scrollView에 여러 개의 단추를 넣으려고하지만 배경보기 만 스크롤합니다. scrollView에 여러 단추를 프로그래밍 방식으로 추가하는 방법

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() 
    } 

은 내가 또한 방법을 시도하지만, 그냥 버튼 스크롤 버튼 프레임 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) 

    } 

} 

를이 코드를 썼다.

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

왜보기 프레임 y는 self.view.frame.size.height을 설정하는? in line view.frame = CGRect (x : 0, y : self.view.frame.size.height, width : self.view.frame.size.width, 높이 : self.view.frame.size.height) –

+0

@ JasmeetKaur Kaur 죄송합니다. 그것에 대해 전혀 모른다. –

+0

ButtonSetup() 함수는 무엇을하고 있습니까? 적어도 하나의 코드를 보여줄 수 있습니까? – DonMag

답변

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!!! 
관련 문제