2015-06-12 27 views
0

我有一個圖像,你可以通過使用UIScrollView滾動。我想添加按鈕。按鈕需要保持附着在圖像上,並且不必單獨滾動。我需要以編程方式完成這一切。如何讓按鈕在Swift中隨背景滾動?

這是代碼,我現在所擁有的:

imageView = UIImageView(image: UIImage(named: "Map 1.png")) 

// 2 
scrollView = UIScrollView(frame: view.bounds) 
scrollView.backgroundColor = UIColor.blackColor() 
// 3 
scrollView.contentSize = imageView.bounds.size 
// 4 
scrollView.addSubview(imageView) 
view.addSubview(scrollView) 

scrollView.contentSize.height = scrollView.frame.size.height 
+1

您需要嵌入圖像視圖和按鈕一個UIView內,然後添加該視圖作爲滾動視圖中的內容 – Paulw11

回答

0

你只需要添加按鈕,滾動型:

var button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
button.frame = CGRectMake(100, 100, 100, 50) 
button.backgroundColor = UIColor.greenColor() 
button.setTitle("Button", forState: UIControlState.Normal) 
button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside) 
scrollView.addSubview(button) 
+2

此方法將使設置滾動視圖的內容大小變得困難 – Paulw11