Preface
After entering the new company. After the one months of reconstruction project, finally the project to achieve the degree of personal relative satisfaction (there is a dissatisfaction with the demand of the boss, have repeatedly commented also useless = =!). In this reconstruction, according to the previous ideas of the design of a personal feel more suitable for a base class. Here the author will put this base class basic design description again.
base class design Requirements
1. At the beginning of our framework, we typically design a Viewcontroller base class and set a random background color in the base class Viewdidload. And through the touch gesture to the interface of the jump, in order to design the beginning of an interface jump frame, and through the interface color changes to verify that our interface jump to do a normal jump.
2. One of the problems that may need to be designed next is the problem with the navigation bar returning button because the system comes with a navigation bar return button that is relatively less attractive. Usually we will customize the back button. Here we use the method to directly implement the redefine navigation bar left button to achieve the effect we want
3. In many cases when we lay out the view sub-view, the extent of the child view may occasionally time out the view bounds range. You may also need to design a method to render the view's sub-view even outside the bounds scope of the view.
VC base class Design implementation
For the above requirements, where demand 1 is the best solution. It is common practice to write a tool class of our own, and then generate a random color as the background color of the base Class View from the tool class in the viewdidload to see if the jump is achieved.
self.view.backgroundColor = [BQTools randomColor];
Next comes the question of customizing the Back button, (most apps now use the navigation bar to launch the next controller if it's present mode.) That ignores this paragraph). Based on the most convenient implementation method, when the view view is loaded, a left column item is generated directly to the navigation bar. and realize its click method to reach the navigation bar pop. "Back" Here is the custom return button view
UIBarButtonItem * leftBarItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarItemAction:)];self.navigationItem.leftBarButtonItem = leftBarItem;
Such a situation is convenient to solve the demand 2 problem, but derived a new problem, that is, when the navigation bar of the first controller will also exist a left column item, and implemented the pop method. So we need to add a decision here to make it in the navigation bar the first controller does not exist for this item. So the updated code is as follows
if ([self.navigationController.viewControllers indexOfObject:self] != 0) { UIBarButtonItem * leftBarItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarItemAction:)]; self.navigationItem.leftBarButtonItem = leftBarItem;}
Finally, the layout problem of the view sub-view, many times we will have beyond the view scope of the child view exists, it is necessary to layout a scrollview on the view view to achieve the view scroll view view bounds sub-view of the purpose. Since this is the case, we can do this here as a sub-view container, directly to a contentview (for ScrollView) in the case of a cell. Finally, through the method of iterating through the frame directly to get the sub-view, the Contentview display area is modified by comparison, and all the sub-views should be added to Contenview
Generate ContentviewSelf.automaticallyadjustsscrollviewinsets =NO;Self.automaticallyadjustsscrollviewinsets =NO;Self.contentview = [[Uiscrollview Alloc] initWithFrame:CGRectMake (0,64, [UIScreen Mainscreen].bounds.size.width, [UIScreen Mainscreen].bounds.size.height-64)]; [Self.view Addsubview:Self.contentview];Self.contentView.contentSize =Self.contentView.bounds.size;In the viewwillappear to judge the scope of the change Contentviewcgfloat contentheight = 0; Nsarray * subviews = self.contentView.subviews; for (UIView * view in subviews) { if (cgrectgetmaxy (view.frame) > contentheight) {contentheight = Cgrectgetmaxy (View.frame); }}//The author's project is primarily scrolling up and down, as the view may be beyond the right side of the view, and can be implemented in the same way if (Contentheight > self.contentView.bounds.size.height { self.contentView.contentSize = cgsizemake (self.contentView.bounds.size.width, contentheight);}
Attention
Some controllers may have the navigation bar background color transparency, at this time, if the navigation bar transparent Contentview layout needs to start from 0, 0, so you also need to give a way to adjust the contentview frame. About the background color adjustment of the navigation bar The author uses a third-party navigation bar
Use range, scene
The above base class is just one of the most basic commonly used prototypes, perhaps in the actual project also need to expand some other common properties, such as the theme color, navigation bar hidden, the tag bar appears hidden, so in the project when used, but also need to eliminate their own actual needs to modify. In addition, as a third requirement, the layout is directly outside the view bounds range, generally because the UI transduction gives a longer graph height. So in the author's project to use the proportional adaptation (not the aspect ratio, but purely based on the width of the contrast ratio in the design) to match this base class, the effect is better (because the author is fully proportional to fit, so the picture will also have a zoom situation, in this case, if not redraw, there may be a waste of efficiency). However, if you are using masonry for layout, it may not be necessary to follow the author's base class design.
Postscript
For the design of the controller base class, the individual has a personal understanding. I am here to throw their own design ideas and part of the code package, I hope you can communicate. If there is any mistake above, please correct me. Thank you!
The base class design for iOS controllers