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

  • Home
  • SEARCH
  • 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 8305895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:13:14+00:00 2026-06-08T18:13:14+00:00

I created a UIView subclass associated with .xib file. This UIView subclass is to

  • 0

I created a UIView subclass associated with .xib file. This UIView subclass is to be used in a UIViewController. In the controller, I think there are two options how we instantiate the UIView subclass:

MyUIView *myView=[[MyUIView alloc] initWithFrame:aRect];

and

MyUIView *myView = [[[NSBundle mainBundle] loadNibNamed:@"MyUIView" 
                                                  owner:self 
                                                options:nil] lastObject]; 

I prefer the first approach or its variants that allow me to perform custom initialization. The only problem is that I have to specify a frame’s rect, which was already specified in .xib (I mean frame’s height and width of MyUIView). Yes, I can hardcode it again in aRect, but this is tedious to maintain (e.g., when I change position of UIs in .xib, I have to update aRect, too).

So the second approach should come into mind as the frame rect is automatically set. The remaining problem is I cannot customize the initializer (say, I want to pass extra parameters during initialization).

What’s your preference? Which one is better in your opinion?

EDIT1:
Inspired by sergio’s answer, I came out with this workaround:

// In MyViewController.m
MyUIView *myView=[[MyUIView alloc] initWithFrame:CGRectMake(x, y, 0.0, 0.0)];

// In MyView.m
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"UnmovableTagView" 
                                              owner:self 
                                            options:nil] lastObject]; 
        [self setFrame:CGRectMake(frame.origin.x, 
                                  frame.origin.y, 
                                  [self frame].size.width, 
                                  [self frame].size.height)]; 
        // frame's width and height already determined after 
        // loadNibNamed was called
       ...
    }
    return self;
}
  • 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-08T18:13:15+00:00Added an answer on June 8, 2026 at 6:13 pm

    Have you tried using:

    MyUIView *myView=[[MyUIView alloc] initWithFrame:CGRectZero];
    

    I don’t know if it works in your case (loading the view from a nib), but I use it successfully when I create my custom view programmatically.

    You can give it a try.

    EDIT:

    you could define your own init method for your view:

    -(id)initWithPosition:(CGPoint)pos;
    

    and then call:

    -(id)initWithPosition:(CGPoint)pos {
    
      if (self = [super initWithFrame:{pos,{0,0}}]) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"UnmovableTagView" 
                                              owner:self 
                                            options:nil] lastObject]; 
        [self setFrame:CGRectMake(frame.origin.x, 
                                  frame.origin.y,  
                                  [self frame].size.width,    
                                  [self frame].size.height)]; 
         // frame's width and height already determined 
         // after loadNibNamed was called
       
      }
     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a subclass of UITableViewCell and added two identical UIView s that have
I have a UIView subclass that I'd like to instantiate from a NIB file.
I created a subclass from UIView #import <UIKit/UIKit.h> @interface MeeSelectDropDownView : UIView { UILabel
I created a UIView and a UILabel outlet for the XIB. In the first
I created a uiview(with two buttons) covers all the screen whenever i click on
I am new to iphone development.I have created a UIview controller class and added
I have created a custom UIView (without .xib) for a finger paint application. Paint
I've created a subclass of UIView called Status which is designed to display a
i need to draw two lines. can i use the same UIView subclass to
I am attempting to create a UIView (and associated UIViewController ) which mimic the

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.