IOS uses xib to define a View and modify the frame to solve the invalid problem. iosxib

Source: Internet
Author: User

IOS uses xib to define a View and modify the frame to solve the invalid problem. iosxib

I have encountered a lot of problems with using custom views and modifying the frame is invalid. Before that, I gave up xib and handwritten it directly. I found that the handwriting is simple and the complicated UI is hard to work. Therefore, xib needs to be visually edited.

Sort it out and forget it for the reference of iOS developers:

 

Generally, we write the following statement directly:

XPGovRecUnitView *recUnitView = [[[NSBundle mainBundle] loadNibNamed:@"XPGovRecUnitView" owner:self options:nil] firstObject];            recUnitView.tag = 10000+i;            recUnitView.delegate = self;            recUnitView.frame = CGRectMake(i*89, 0, 89, 139);

  

This is the code in my project, but the iPhone 6, 6 plus and above are normal, and the iPhone 5S screen size is not normal.

Use

UIView * recUnitView = [[UIView alloc] initWithFrame: CGRectMake (I * 89, 0, 89,139)];

After debugging, it is found that the alloc method is also normal for iPhone 5. But in this case, you need to hand-write the code and add the control to this UIView.

 

Solution:

1. Set the properties of the xib file, XPGovRecUnitView. xib.

In the attribute column on the right,
Find the Interface Builder Document and remove the Use Auto layout check box.
Find the Simulated Metrics and set the Size to None. If there is no None, it is Freeform.

2. Modify the XPGovRecUnitView. m code

  

#import "XPGovRecUnitView.h"@interface XPGovRecUnitView (){    CGRect tempframe;}@end@implementation XPGovRecUnitView-(id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"XPGovRecUnitView" owner:nil options:nil];        self=[nibs objectAtIndex:0];                tempframe = frame;                [self initSubViews];    }    return self;}-(void)drawRect:(CGRect)rect{    self.frame = tempframe;    }@end

  

3. Use the time code

  

XPGovRecUnitView *recUnitView = [[XPGovRecUnitView alloc] initWithFrame:CGRectMake(i*89, 0, 89, 139)];            recUnitView.tag = 10000+i;            recUnitView.delegate = self;

  

This is normal.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.