UIScrollView的子视图不调整大小(手动)

时间:2014-11-11 18:15:24

标签: ios swift uiscrollview resize subview

我有一个带有UIView的UIScrollView。当我尝试通过代码手动调整UIView的大小时,它不起作用。我尝试将UIView的autoResizingMask属性设置为.None,并且我实现了UIScrollView的touchesShouldCancelInContentView方法以返回true,但它仍然不起作用。这是我的代码:

class ViewController: UIViewController {

@IBOutlet var scrollView: CustomScrollView!
@IBOutlet var field: UITextField!



override func viewDidLoad() {
    super.viewDidLoad()

    var gesture = UITapGestureRecognizer(target: self, action: "gestureHandler")
    gesture.numberOfTapsRequired = 1
    view.addGestureRecognizer(gesture)

    scrollView.contentSize = CGSizeMake(430, 140)

    field.autoresizingMask = .None

}

func gestureHandler() {

    field.frame.size.width += 20

    var view = UIView(frame: CGRectMake(0, 0, 30, 100))
    view.backgroundColor = UIColor.redColor()

    field.frame.size.width -= 30
    field.frame.origin.x += 30

    scrollView.addSubview(view)

}

}

class CustomScrollView: UIScrollView {

override func touchesShouldCancelInContentView(view: UIView!) -> Bool {

    return true

}


}

我得到了什么:

我想要的是:

感谢您的帮助!

0 个答案:

没有答案