objective c - how Set autolayout for multiple dynamic buttons and labells in iOS? -


I am creating 83 dynamic buttons in my app and setting the label for it. Now, I want to set auto layout for that. How can I set up the automatic layout for this?

This is the code that I have implemented to create buttons and labels dynamically:

  - (void ) Dynamic Button {// Reading data from database NSMutableArray * objectName = [[NSMutableArray alloc] in this]; NSArray * docPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDir = [docPathsObjectAttex: 0]; Self.databasePath = [documentDir stringbappingSpaceContent: @ "SMS.sqlite"]; FMDatabase * Database = [FMDatabase database path: self.databasePath]; [Database logs error: TRUE]; [Database open]; // FMResultSet * Result = [Database Execution: @ "Select from SMS Card"]; NSString * anQuery = [[NSString alloc] initWithFormat: @ "SELECT * FROM SMSCategory"]; FMResultSet * Result = [Database Execution: anQuery]; While ([result next]) {SMSCat1 * cat = [[SMSCat1 alloc] init]; Cat.Id = [Results stringForColumn: @ "Id"]; Cat.Name = [stringforction result: @ "name"]; Cat.IsActive = [Results stringForColumn: @ "Inactive"]; [Object node addObject: cat]; } [Close the database]; // add dynamic button ypossion = 150, xPossion = 44; Int temp = 0; UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: self.view.frame]; [Self.view addSubview: scrollView]; For (Int i = 0; I & lt; [Object count count]; i ++) {SMSCat1 * cat = [Object named ObjectAntIndex: I]; UIButton * aButton = [UIButtonTypeCustom] with UIButton Button; [One button set transformsautoorsisizing mask intensitins: Yes]; // Label dynamic code UILabel * label = [[UILabel alloc] init]; [Label set text: cat.Name]; [Label SettextColver: [UIColor browncolor]]; Label.font = [UIFont systemFontOfSize: 12]; [Label size tofitt]; [Label set frame: CGRactack (5, 44, 70, 60)]; [Add ScrollView Subview: Labels]; [Abton aad sublueau: label]; [One button setbackgram color: [UIColor blackcolor]]; [One button setbackground image: [UIImage imageNamed: @ "icon-menu.png"] forState: UIControlStateNormal]; [One Button Sethital: [NSString stringWithFormat: @ "% d", i] forState: UIControlStateNormal]; [One button setframe: CGRactack (exposion, Yopson, 70, 60)]; Abutton.highlighted = yes; [Add ScrollView Subview: aButton]; XPossion + abutton.frame.size.width + 35; Temporary ++; If (temp == 3) {yPossion = aButton.frame.origin.y + aButton.frame.size.height + 20; Temp = 0; XPossion = 44; YPossion + abutton.frame.size.width - 15; [ScrollView Set Content Size: CGSizeMake (scrollView.frame.size.width, yPossion - 50)]; }}}}    

When using the automatic layout for this, you SetTranslatesAutoresizingMaskIntoConstraints want to set: No . This allows you to determine the position of objects using your obstacles. Keeping the object using auto layout does not really care about frames about you. You want to place objects around objects or objects of their parent example, for example:

  NSDictionary * views = @ {@ "label1": label1, @ "Button1": button}; [Self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "[label 1] - (10) - [button1]" Options: 0 metric: zero views: views]]; [Self.view addConstraint: [NSLayoutConstraint constraintWithItem: label attribute: NSLayoutAttributeCenterY Related: NSLayoutRealationEqual toItem: Button Feature: NSLayoutAttributeCenterY Multiplier: 1.0 Continuous: 0]];   

This will separate the two elements from 10 pixels and focus them vertically. I'm not going through my code and it changes everyone on auto layout but in my example how do you start laying your object with the layouts

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -