问题描述:

I am trying to re-write my code to allow me to use xibs to design custom UIViews for re-use throughout my projects. I have created a class called SlidesView which has a .xib of the same name. I have set it up using the following IBOutlets:

@property (nonatomic, retain) IBOutlet UIImageView *ivMain;

@property (nonatomic, retain) IBOutlet UILabel *lHeader;

@property (nonatomic, retain) IBOutlet UITextView *tvMain;

@property (nonatomic, retain) IBOutlet UIImageView *ivIcon;

I have made sure these are all properly connected within the .xib file. I also have a function which I want to use to setup all the initial data for the view:

- (void)setupUsingCenter:(CGPoint)center mainImage:(UIImage *)mainImage mainText:(NSString *)mainText header:(NSString *)header andIconImage:(UIImage *)iconImage

The 'setupUsingCenter:self ...' body looks as follows:

// First check for a main image and image icon

if (mainImage != nil)

{

// Set the image

[self.ivMain setImage:mainImage];

[self.ivMain setFrame:CGRectMake(10, 10, 10, 10)];

// If image icon set that too

if (iconImage != nil)

{

[self.ivIcon setImage:iconImage];

}

}

And finally I load this .xib using the following code:

NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"SlidesView" owner:nil options:nil];

for (UIView *view in views) {

if([view isKindOfClass:[SlidesView class]])

{

SlidesView *slidesView = (SlidesView *)view;

[slidesView setupUsingCenter:self.vPlaceholder.center mainImage:slideImage mainText:slideText header:slideTitle andIconImage:slideIcon];

[slidesView setTextColours:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f]];

[self.view addSubview:slidesView];

self.viewPopup = slidesView;

}

}

I have put in the random setFrame parameters for testing purposes but for some reason the frame doesn't change. However, the image on the ivMain does change. Can anyone explain how I can change the frame for my UI objects? Also, why does it allow me to change the image but the frame seems un-editable, doesn't this mean it must be initialised?

Any help is much appreciated,

Thanks.

网友答案:

Are you sure autolayout is disabled on the .xib ? It is my understanding that you cannot edit frames when autolayout is on.

If that's the case, you can either disable autolayout for that particular .xib, or you can work on constraints instead of the frame property

网友答案:

The use of autosizing may cause some unexpected behaviors also the autoresize subviews.

You can try something like this:

[View setAutoresizingMask:UIViewAutoresizingNone];

And:

[View setAutoresizesSubviews:NO];

Where View is the UIView that you want to apply that behavior.

相关阅读:
Top