简体   繁体   中英

UIView in a custom xib

I created a UIView in a separate subclass of UIView and with an xib.

I have a UIViewController with a UIView in storyboard and I want to set custom UIView to this UIView.

I modified class name in storyboard UIView to the class name of custom UIView. I am getting the UIView on the result but with the frame size of custom UIView. I added correct constraints in the custom 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.

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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