having a custom UIView in a UIViewcontroller

2019-09-02 10:49发布

I created a UIView class and implemented it inside a viewcontroller. However, I have a some other things in the view controller that I want to be able to interact with but I cannot do that because of the attached view.

how do I interact with the base viewcontroller while the custom uiview is present in the viewcontroller

var tripView: TripView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tripView = TripView(frame: CGRect.zero)
        self.view.addSubview(tripView)
        // AutoLayout
        tripView.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets.zero)

    }

2条回答
看我几分像从前
2楼-- · 2019-09-02 11:48

You can do

class TripView:UIView {

    // add this
class TransView:UIView {

    var imgV1:UIImageView!
    var imgV2:UIImageView!

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {

        return imgV1.frame.contains(point) ||  imgV2.frame.contains(point) // or ![imgV1,imgV2].filter{$0.frame.contains(point)}.isEmpty
    }

}
查看更多
女痞
3楼-- · 2019-09-02 11:51

You should assign proper index to your views by using following methods from UIView.
Instance Methods: aboveSubview and belowSubview or insertSubview .

From UIView overview

Views can be nested inside other views to create view hierarchies, which offer a convenient way to organize related content. Nesting a view creates a parent-child relationship between the child view being nested (known as the subview) and the parent (known as the superview). A parent view may contain any number of subviews but each subview has only one superview. By default, when a subview’s visible area extends outside of the bounds of its superview, no clipping of the subview's content occurs.
Hope it helps!

查看更多
登录 后发表回答