制作一个填充其超级视图的自定义UIView子视图 [英] Making a custom UIView subview that fills its superview

查看:105
本文介绍了制作一个填充其超级视图的自定义UIView子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用自己的初始化程序编写了自己的自定义视图。但是,当我的主视图加载我的自定义视图时,会以错误的方式进行描述。它的边界为600x600矩形,而superview为375x607。我确实试图把自动约束,似乎没有工作。我尝试在子视图初始化中以编程方式执行此操作,但每当我尝试将其bounds属性初始化为其superview边界时,我在superview中得到nil。

So I wrote my own custom view with its own initializer. However, when my main view loads my custom view gets depicted in a wrong way. It takes bounds as 600x600 rectangle, while the superview is 375x607. I did try to put auto constraint, seems not to work. I tried to do it programmatically in the subview initialization, but whenever I try to initialize it's bounds property to its superview bounds I get nil in superview.

override init(frame: CGRect) {
    super.init(frame: frame)
    self.setup()
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.setup()
}

func setup() {
    initPathLayer()
    initHandleView()
    initHandlePanGestureRecognizer()

    layoutPathLayer()
    layoutHandleViews()
}

我尝试了互联网上的所有内容,使子视图填充其超级视图,但我认为子视图在superview之前被初始化了?那可能吗 ?在ViewController中,我将自定义视图声明为Outlet连接。我确信问题应该非常简单,而且我不知道Swift初始化视图的方式。

I tried everything there is on the internet to make subview fill its superview, but I think that subview gets initialized before superview? Is that possible ? In the ViewController a have my custom view declared as an Outlet connection. I'm sure that the problem should be super easy and it's me who doesn't know the way Swift initializes the view.

任何想法?

谢谢。

推荐答案

首先,一个Views init方法不是最好的执行方法布局。视图可以在稍后调整大小,如果视图是从Xib加载的,或者您是在View Controllers viewDidLoad 函数中创建它,则通常总是这样。

Firstly, a Views init method is not the best place to perform a layout. The view could be resized at a later point which is typically always the case if the view is loaded from a Xib or you are creating it in a View Controllers viewDidLoad function.

据说你有几种方法:

这可以在Interface Builder中以编程方式完成。在Interface Builder中,您只需使用Pin选项并选择视图的所有边

This can be done either in Interface Builder or programmatically. In Interface Builder you simply use the 'Pin' option and select all the sides of the view

完成此操作后,您应该能够在大小检查器中看到您的约束,如下所示:

When this is done you should be able to see your constraints in the Size inspector looking as follows:

或者,您始终可以在初始化程序中以编程方式添加约束:

Alternatively you can always add your constraints programmatically in your initializer:

override init(frame: CGRect) {
    let view = UIView(frame: .zero)
    view.translatesAutoresizingMaskIntoConstraints = false
    super.init(frame: frame)
    let viewsDict = ["view": view]
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: .allZeros, metrics: nil, views: viewsDict))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: .allZeros, metrics: nil, views: viewsDict))
    addSubview(view)
}

convenience required init(coder aDecoder: NSCoder) {
    self.init(frame: .zero)
}



2。手动布局



调整面具大小



为此,您可以使用调整大小的面具,也可以在layoutSubviews中控制框架。调整掩码的大小告诉系统视图应如何相对于superview调整大小:

2. Manual Layout

Resizing Masks

For this you can either use resizing masks or you can control the frame in layoutSubviews. Resizing masks tell the system how the view should be sized relative to the superview:

override init(frame: CGRect) {
    let view = UIView(frame: CGRectZero)
    view.translatesAutoresizingMaskIntoConstraints = false
    super.init(frame: frame)
    view.frame = bounds
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    addSubview(view)
}



布局子视图



最后,您可以覆盖layoutSubviews并从那里开始:

Layout Subviews

Lastly, you can override layoutSubviews and go from there:

override func layoutSubviews() {
    super.layoutSubviews()
    view.frame = bounds
}

这篇关于制作一个填充其超级视图的自定义UIView子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆