简体   繁体   English

UIView在自定义xib中

[英]UIView in a custom xib

I created a UIView in a separate subclass of UIView and with an xib. 我在UIView的一个单独的子类中创建了一个UIView,并使用了一个xib。

I have a UIViewController with a UIView in storyboard and I want to set custom UIView to this UIView. 我在故事板中有一个带有UIView的UIViewController,我想为这个UIView设置自定义UIView。

I modified class name in storyboard UIView to the class name of custom UIView. 我将故事板UIView中的类名修改为自定义UIView的类名。 I am getting the UIView on the result but with the frame size of custom UIView. 我在结果上获得了UIView,但是使用了自定义UIView的帧大小。 I added correct constraints in the custom xib. 我在自定义xib中添加了正确的约束。 and some touch gestures not working as desired because of the wrong frame etc. 由于错误的框架等,一些触摸手势无法正常工作

Please suggest me if anything wrong I did. 如果我做错了,请建议我。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self) {

        // Load the UIView from Interface Builder
        [[NSBundle mainBundle] loadNibNamed:@"TMPhotoEdit" owner:self options:nil];

        // Add UIView to current UIView object.
        [self addSubview:self.view];
    }

    return self;
}

self.view is the same view object I created a variable for it. self.view是我为它创建变量的相同视图对象。

I want the frame to set as same as the one i am using in storyboard. 我希望框架设置与我在故事板中使用的框架相同。

Solution to get it work using code also helps me a lot. 使用代码使其工作的解决方案也对我有很大帮助。 I mean, without IBOutlet in storyboard, just adding this UIView with some frame set to it. 我的意思是,如果没有故事板中的IBOutlet,只需添加这个UIView并设置一些框架。

If I call initWithFrame, I can't set the xib like what I am doing with initWithCoder above and initWithCoder is not being called if I use initWithFrame in code. 如果我调用initWithFrame,我不能像上面的initWithCoder那样设置xib,如果我在代码中使用initWithFrame,则不会调用initWithCoder。

假设您有一个想要大小相似的视图实例

self.view.frame = CGRectMake(otherView.frame.x, otherView.frame.y, otherView.frame.width, otherView.frame.height);

Actually what you are doing is not correct but anyway you should load the custom view nib like this 实际上你正在做的事情是不正确的,但无论如何你应该像这样加载自定义视图笔尖

self.view = [[NSBundle mainBundle] loadNibNamed:@"TMPhotoEdit" owner:self options:nil][0];

Again this is not correct, why do you need it anyway? 这又不正确,你为什么还需要呢? but it shall solve your nib loading issue. 但它应解决你的笔尖装载问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM