IOS custom control development (in progress), ios Control
IOS custom control development (I)
IOS custom control development (in progress)
Next, after developing the iOS custom control, let's try another one.
On the right side of Xcode, the following figure is displayed:
There is a M Class above.
That means we can directly use this m Class to customize our Class ~
#0 first attempt
Enter the Class field in the Custom Control name we wrote.
Then modify the ChartViewController file we wrote last time. Delete the previous association and re-associate the View.
//// ChartViewController. h /// Created by Luna Gao on 15/10/29. // Copyright©2015 zhiqiankeji.com. all rights reserved. // # import <UIKit/UIKit. h> # import "LGChartView. h "# import" Hardware. h "@ interface ChartViewController: UIViewController @ property (weak, nonatomic) IBOutlet LGChartView * chartView; @ end
Delete all the instantiated code in viewDidLoad, just like using native controls such as UILabel, and no longer instantiate objects.
- (void)viewDidLoad { [super viewDidLoad];}
Then modify the LGChartView and delete the previously defined
- (instancetype)initWithParentView:(UIView*) view;
Method. The declaration and implementation must be deleted.
Then run our code again. The following situation occurs:
Many methods have been tried for modification, but none of them are successful. The first attempt failed.
#1 second attempt
After analysis, the reason is that the background color is not set during initialization, that is:
- (instancetype)initWithFrame:(CGRect)frame{ frame = CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height); self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.autoresizesSubviews = YES; self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; } return self;}
In the previous code
self.backgroundColor = [UIColor clearColor];
This code is not executed.
Try to put it in the init method without executing it.
Try to put it in the initWithFrame method and do not execute it.
Put it in the initWithCoder method. Success! (Wipe !!! I tried this method when I was bored, and the result was successful. I never used this method before ···)
This is much simpler ~
You can delete all the previously set adaptive code.
self.autoresizesSubviews = YES; self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
Delete useless code, simplify it, and accidentally delete
-(void)layoutSubviews { [self setNeedsDisplay];}
The following problems occur:
It can be seen that after the interface is rotated, deformation occurs. Therefore, keep this method.
#2 Ultimate Solution
Based on the above:
Custom Control, inherited from UIView, modify the Custom Class in Storyboard and bind it to our Custom control.
The custom control must implement the initWithCoder method. Storyboard automatically calls this method for initialization.
Then, modify the drawRect method to draw the custom control content.
Finally, do not forget to call setNeedsDisplay in the layoutSubviews Method for Refresh.
At this point, the custom control is completed in the instantiation phase. We can use it in a very simple and convenient way.