having a custom UIView in a UIViewcontroller

☆樱花仙子☆ 提交于 2019-12-25 00:12:27

问题


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)

    }

回答1:


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
    }

}



回答2:


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!



来源:https://stackoverflow.com/questions/54648327/having-a-custom-uiview-in-a-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!