我有以下代码。我添加了一个按钮,当我按下它时想要发生一些事情,但它似乎不起作用。在这种情况下,我希望系统打印出来" hi"并且"嘿"。我似乎无法弄清问题。任何帮助,将不胜感激。
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var containerView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
var buttonOne: UIButton!
buttonOne = UIButton(type: UIButtonType.System)
buttonOne.setTitle("Level 1", forState: UIControlState.Normal)
buttonOne.frame = CGRectMake(10, 50, 100, 100)
buttonOne.backgroundColor = UIColor.blueColor()
buttonOne.titleLabel?.font = UIFont.systemFontOfSize(17)
buttonOne.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
buttonOne.addTarget(self, action: Selector("clicked:"), forControlEvents: UIControlEvents.TouchUpInside)
self.scrollView = UIScrollView()
self.scrollView.delegate = self
self.scrollView.contentSize = CGSizeMake(0, 1000)
containerView = UIView()
scrollView.addSubview(containerView)
view.addSubview(scrollView)
containerView.addSubview(buttonOne)
}
func clicked(sender: AnyObject){
NSLog("Hey")
print("hi")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:0)
通过将其bounds大小设置为scrollview的contentSize,将containerView的宽度设置为0
self.scrollView.contentSize = CGSizeMake(0, 1000)
那么
containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width,
scrollView.contentSize.height)
答案 1 :(得分:0)
您已将按钮放在滚动视图中。这里,scrollview是按钮的超级视图,scrollview的宽度及其内容大小宽度必须大于或等于要执行操作的按钮宽度。