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.