斯威夫特:在故事板和大小限制可重​​复使用的UIView [英] Swift: Reusable UIView in storyboard and sizing constraints

查看:129
本文介绍了斯威夫特:在故事板和大小限制可重​​复使用的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造斯威夫特一个可重用的UIView,我可以插入我的故事板视图控制器。我的主要问题,现在是可重复使用的UIView小工具不完全配合到故事板UIView的框。我跟着<一个href=\"https://www.youtube.com/watch?v=o3MawJVxTgk&list=PLByZM902Jo_ax-JbkizIduWOn5nj5mPNL&index=1\">this教程建立可重复使用的UIView控件

I'm trying to create a reusable UIView in Swift that I can plug into my Storyboard view controllers. My key issue right now is that the reusable UIView "widget" doesn't fully fit into the UIView box in the storyboard. I followed this tutorial to set up the reusable UIView widget


  1. 创建的UIView和相应的.xib的一个子类 - 以及连接这些:

  1. Created a subclass of UIView and a corresponding .xib -- and connected these:

import UIKit

class MyWidgetView: UIView {

@IBOutlet var view: UIView!;

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);

NSBundle.mainBundle().loadNibNamed("MyWidgetView", owner: self, options: nil);
self.addSubview(self.view);
}

}


  • 在厦门国际银行,这是对应于code上面的接口文件,我使用的UIView下的模拟度量自由曲面的大小和规模在浏览模式来填充。

  • In the XIB, which is the interface file corresponding to the code above, I used UIView with Freeform size under the Simulated Metrics, and Scale to Fill under View mode.

    在主故事板,我添加了一个UIView块(同一矩形),并改变了Class,以MyWidgetView

    In the main storyboard, I added a UIView block (same rectangular shape) and changed the Class to MyWidgetView

    它的工作原理,但组件我在厦门国际银行看在实际应用压扁,创造尽管我使用布局约束在这两个XIB和事实也是主要的故事板。

    It works, but the components I created in the XIB look squished in the actual app, despite the fact that I used layout constraints in both the XIB and also the main storyboard.

    见截图。粉红色的部分是不应该出现,因为这仅仅是一个上,我加入到测试上浆的主要故事板的UIView的颜色。这UIView的其实是MyWidgetView(后我在第3步因此理论上改变了类,因为MyWidgetView ==主故事板的UIView,这UIView的具有约束,使其在上海华长方形,那么为什么我的小部件压扁?下面的蓝色部分应该扩展权的所有道路。

    See the screenshot. The pink part isn't supposed to appear, since that is just a color of the UIVIew on the main storyboard that I added to test the sizing. That UIView is actually MyWidgetView (after I changed the class in step 3. So in theory, since MyWidgetView == the UIView on the main storyboard, and that UIView has constraints that make it rectangular in the superview, then why is my widget squished? The blue part below should extend all the way right.

    推荐答案

    在您的code从笔尖文件加载实际的视图层次是通过添加
    self.addSubview(self.view)。所以,帧的 self.view 竟与其父,即没有关系 MyWidgetView

    The actual view hierarchy loaded from the nib file in your code is added via self.addSubview(self.view). So, the frame of your self.view actually has no relationship with its parent, i.e. MyWidgetView.

    您可以选择要么通过code添加布局约束或添加作为一个子视图后,只设置它的框架。就个人而言,我preFER后者。在我的实验,以下是我的作品。我用X code 6.4,我认为这是不一样的一个作为你的。

    You may choose either adding layout constraints through code or just setting its frame after being added as a subview. Personally, I prefer the latter. In my experiment, the following is what works for me. I am using Xcode 6.4, which I think is not the same one as yours.

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    
        if let nibsView = NSBundle.mainBundle().loadNibNamed("MyWidgetView", owner: self, options: nil) as? [UIView] {
            let nibRoot = nibsView[0]
            self.addSubview(nibRoot)
            nibRoot.frame = self.bounds
        }
    }
    

    这篇关于斯威夫特:在故事板和大小限制可重​​复使用的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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