Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the automatic Generation of views in Nine Palace grids by iOS

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how iOS realizes automatic view generation of Jiugong grid". The content is simple and easy to understand and organized clearly. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how iOS realizes automatic view generation of Jiugong grid".

First of all, there must be a class to manage the whole module, so create a UISdokuView class to inherit from UIScrollView: --Why scollView? -- Because when the number of controls to be added is large, it will obviously exceed the scope of the mobile phone screen, and only scrollView can be fully displayed, that is to say, as long as the user provides the frame of a single control, the number of controls, and the number of controls in each row, you can determine the contentSize of the UIScrollView and add it.

UISdokuView class

.h documents

@interface UISodokuView : UIScrollView//frame@property of base control (nonatomic,assign)CGRect itemFrame;//Number of controls to add @property (nonatomic,assign)NSInteger itemsNumber;//Number of controls per line @property (nonatomic,assign)NSInteger itemsNumberInOneLine;//array@property that stores the control (nonatomic,strong)NSMutableArray *itemsArray;//scrollView width @property (nonatomic,assign)NSInteger scrollViewWidth;//scrollViewHeight @property (nonatomic,assign)NSInteger scrollViewHeight;//Initialized, but no control added-(instancetype)initWithItemFrame:(CGRect)frame andItemsNumber:(NSInteger)itemsNumber andItemsNumberInOneLine:(NSInteger)itemsInOneLine;

Each control I add to scrollView is a UIView object with a white background by default and stored in itemsArray. What the user wants each control to display can be added by getting an array object.

.m file

@implementation UISodokuView-(instancetype)initWithItemFrame:(CGRect)frame andItemsNumber: (NSInteger)itemsNumber andItemsNumberInOneLine: (NSInteger)itemsInOneLine{ self = [super init]; if (self) { //Initialize_itemsArray = [NSMutableArray array]; _itemFrame = frame; _itemsNumber = itemsNumber; _itemsNumberInOneLine = itemsInOneLine; self.frame = CGRectZero; } [selflayoutModule]; return self;}-(void)layoutModule{ //Gets item width, height and horizontal vertical spacing NSInteger itemWidthGap = _itemFrame.origin.x; NSInteger itemHeightGap = _itemFrame.origin.y; NSInteger width = _itemFrame.size.width; NSInteger height = _itemFrame.size.height; //Container width_scrollViewWidth = itemWidthGap *(_itemsNumberInOneLine + 1) + width * _itemsNumberInOneLine; //Total Number of Lines NSInteger numberOfLines = 0; if (_itemsNumber%_itemsNumberInOneLine == 0) { numberOfLines = _itemsNumber/_itemsNumberInOneLine; }else{ numberOfLines = _itemsNumber/_itemsNumberInOneLine + 1;} _scrollViewHeight = itemHeightGap*(numberOfLines + 1) + height*numberOfLines - 2; //Determines scrollView frame, default y-axis margin 200 self.frame = CGRectMake (0, 200, _scrollViewWidth,height + itemHeightGap*2); self.contentSize = CGSizeMake (_scrollViewWidth, _scrollViewHeight); self.scrollEnabled = YES; self.backgroundColor = [UIColor lightGrayColor]; //Create and add controls NSInteger line = 1; NSInteger row = 1; for (int i = 1;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report