2

I created MyCustomView which has two labels. One at left and one with right align and at right. It has about 30px height (it's not important). I connect it to xib and it works. In .xib I set main view width to 400px and set autolayout constraints for labels.

Now the problem. When I added to my controller in IB with UIView and set class to MyCustomView I see left label but right label is out of screen. I set constraints right but It doesn't work. When I tried to resize MyCustomView in .xib by mouse and moving with edges it's okay but when I set width value "manually" in right column it doesn't layout right (it doesn't change at all). Where is the problem? How can I fix this?

@implementation MyCustomView

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

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    self.backgroundColor = [UIColor clearColor];

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

    [self addSubview:self.view];
    //self.translatesAutoresizingMaskIntoConstraints = NO;
}

- (void)awakeFromNib {
    [super awakeFromNib];

    //[self setNeedsUpdateConstraints];
    //[self setNeedsLayout];
}

@end

As you can see in comments I tried some ways but nothing helped.

4
  • You said you have set autolayout constraints for labels, can you explain what you did exactly briefly? Commented Jan 22, 2015 at 9:43
  • For right label I set constraints: width to 100, top, bottom and trailing space to 0. Commented Jan 22, 2015 at 9:49
  • Shouldn't you add constraints to your customview itself after adding it to your superview? Commented Jan 31, 2015 at 1:04
  • I'm a bit confused by your setup here. MyCustomView has a corresponding XIB, right? If so, how come you're manually loading the XIB inside commonInit? If MyCustomView is a UIView, what does self.view reference? Commented Feb 1, 2015 at 1:31

1 Answer 1

1
  1. New File --> iOS (Source) -- Cocoa Touch --> Next New File

  2. Enter Name MyCustomView

  3. Add code in MyCustomView.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //self.view.backgroundColor = [UIColor redColor];
        self.preferredContentSize = CGSizeMake(30.0, 400.0);
    }
    return self;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.