Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8863437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:58:39+00:00 2026-06-14T15:58:39+00:00

I have subclassed UIView to make a custom groupView I use to add a

  • 0

I have subclassed UIView to make a custom groupView I use to add a few things to my layout in a simple way. This groupView includes a UILabel which is used as a heading and a UIView that draws a roundRect on it’s CALayer with a background color. Think UITableView’s sections. I add this groupView to the storyboard by dropping a UIView and changing it’s class to my subclass. All works well, I set the heading via the User defined runtime attributes in Xcode. All works great, I add UILabels to this view on the storyboard and it creates the heading label and roundrect when it runs.

structure of my groupView:

  1. groupView: (UIView)clipToBounds:NO;
    • heading: (UILabel) positioned
      above the groupView.
    • contentView:(UIView) creates the roundRect
      and color via CALayer, should be same size as the groupView.

So what’s the problem? Well, dealing with autolayout is a pain to begin with, but for this subclassed UIView to work I need to set the contentView constraints programmatically. I can’t figure out the syntax of this auto layout ASCII format string. Currently I have:

 _contentView = [[UIView alloc]initWithFrame:self.bounds];

    _contentView.layer.cornerRadius = 5.0f;
    _contentView.layer.masksToBounds=YES;
    _contentView.backgroundColor=_backgroundColor;
    _contentView.layer.borderWidth=_borderWidth;
    _contentView.layer.borderColor=_borderColor.CGColor;
    [self insertSubview:_contentView atIndex:0];
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(self,_contentView);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[self]-0-[_contentView]-0-[self]" options:0 metrics:nil views:viewsDictionary];

    for (NSLayoutConstraint *constraint in constraints) {
        [_contentView addConstraint:constraint];
    }

Which crashes with: * Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That’s illegal. constraint: view:>’

I tried this first and it still didn’t work:

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_contentView);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[_contentView]-0-|" options:0 metrics:nil views:viewsDictionary];

Which crashes with: * Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That’s illegal. constraint: view:>’

RANT: Somehow this AutoLayout is suppose to save us work, but
I do not see how the benefits out weight the overhead right now. Why
on earth did they move from using references and methods or even type
defs to this archaic format string? How much easier would it be to do:
[_contentView constraint:NSLayoutFormatAlignLeading toView:self
withDistance:0.0f]; OR something similar? I would so much rather deal
with springs and struts at this point.

Any help understanding, or showing me the syntax to constrain the contentView to the size of self would be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:58:40+00:00Added an answer on June 14, 2026 at 3:58 pm

    The error tells you what you need to know:

    Does the constraint reference something from outside the subtree of the view?

    Yes it does. It references its superview.

    These constraints you are making need to be applied to the superview, in this case the groupView.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom subclass of UIView, LineDrawingView. I have this as a @property
I have this in a custom UIView subclass init function: self.clearsContextBeforeDrawing=YES However, each time
I have subclassed UIView so I can draw some lines in it. In this
I have subclassed UIView object inside a uiscrollview which displays a pdf page. I
I have subclassed a UIView . I am adding it as a subview of
I have a UIView subclass ( CustomView for purposes of this question) that has
I have subclassed a control in C# WinForms, and am custom drawing text in
I didn't use subviews but painted my things with -drawRect: inside an UIView subclass.
I have a subclassed UIView loaded from a nib, and I cannot get a
Is there a way to make a custom animation when clicking on an iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.