changeset 2829:c543f7df7867

iOS: Implement layout constraints to put the UIStackView containing the columns below the row's image and text label and attempt MULTI mode. Need to figure out how to add the stack's extra height to the cell. Multi mode is implemented with UIButtons and I am getting a weird selector sent to the buttons when in Multi mode.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 06 Sep 2022 07:07:19 +0000
parents 2c64d04abcf1
children cc9258ba53d7
files ios/dw.m
diffstat 1 files changed, 56 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/ios/dw.m	Sun Sep 04 16:54:57 2022 +0000
+++ b/ios/dw.m	Tue Sep 06 07:07:19 2022 +0000
@@ -2358,65 +2358,99 @@
         /* If we don't have a stack, create one */
         if(!stack)
         {
+            NSLayoutConstraint *constraint;
+
             stack = [[[UIStackView alloc] init] retain];
             [stack setTranslatesAutoresizingMaskIntoConstraints:NO];
             [stack setSpacing:5.0];
             [[self contentView] addSubview:stack];
-        }
-        /* Extra mode creates a horizontal stack with all the column data */
-        if(_dw_container_mode == DW_CONTAINER_MODE_EXTRA)
-        {
+
+            /* Leading */
+            constraint = [NSLayoutConstraint constraintWithItem:stack attribute:NSLayoutAttributeLeft
+                                                      relatedBy:NSLayoutRelationEqual toItem:[self contentView]
+                                                      attribute:NSLayoutAttributeLeft multiplier: 1.0 constant:0.0];
+            [[self contentView] addConstraint:constraint];
+
+            /* Top */
+            constraint = [NSLayoutConstraint constraintWithItem:stack attribute:NSLayoutAttributeTop
+                                                      relatedBy:NSLayoutRelationEqual toItem:[self textLabel]
+                                                      attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
+            [[self contentView] addConstraint:constraint];
+        }
+        /* Extra and Multi modes create a stack with all the column data */
+        if(_dw_container_mode > DW_CONTAINER_MODE_DEFAULT)
+        {
+            bool extra = _dw_container_mode == DW_CONTAINER_MODE_EXTRA;
             id subviews = [stack arrangedSubviews];
             int index = 0;
 
-            [stack setAxis:UILayoutConstraintAxisHorizontal];
+            /* Extra mode stack is horizontal, multi stack is vertical */
+            [stack setAxis:(extra ? UILayoutConstraintAxisHorizontal : UILayoutConstraintAxisVertical)];
+
             /* Create the stack using columndata, reusing objects when possible */
             for(id object in columndata)
             {
                 if([object isKindOfClass:[NSString class]])
                 {
-                    UILabel *label = nil;
-
+                    id label = nil;
+
+                    /* Check if we already have a view, reuse it if possible.. */
                     if(index < [subviews count])
                     {
                         id oldview = [subviews objectAtIndex:index];
 
-                        if([oldview isMemberOfClass:[UILabel class]])
+                        if([oldview isMemberOfClass:(extra ? [UILabel class] : [UIButton class])])
+                        {
                             label = oldview;
+                            /* If we are reusing a button, make sure the image is not set */
+                            if(!extra)
+                                [label setImage:nil forState:UIControlStateNormal];
+                        }
                         else
                             [stack removeArrangedSubview:oldview];
                     }
                     if(!label)
                     {
-                        label = [[UILabel alloc] init];
+                        label = extra ? [[UILabel alloc] init] : [UIButton buttonWithType:UIButtonTypeCustom];
 
                         [label setTranslatesAutoresizingMaskIntoConstraints:NO];
-                        [label setTextAlignment:NSTextAlignmentCenter];
+                        if(extra)
+                            [label setTextAlignment:NSTextAlignmentCenter];
 
                         if(index < [subviews count])
                             [stack insertArrangedSubview:label atIndex:index];
-                        else
+                        else /* Remove the view if it won't work */
                             [stack addArrangedSubview:label];
                     }
-                    [label setText:object];
+                    /* Set the label or button text/title */
+                    if(extra)
+                        [label setText:object];
+                    else
+                        [label setTitle:object forState:UIControlStateNormal];
                     index++;
                 }
                 else if([object isMemberOfClass:[UIImage class]])
                 {
-                    UIImageView *image = nil;
-
+                    id image = nil;
+
+                    /* Check if we already have a view, reuse it if possible.. */
                     if(index < [subviews count])
                     {
                         id oldview = [subviews objectAtIndex:index];
 
-                        if([oldview isMemberOfClass:[UIImageView class]])
+                        if([oldview isMemberOfClass:(extra ? [UIImageView class] : [UIButton class])])
+                        {
                             image = oldview;
-                        else
+                            /* If we are reusing a button, make sure the text is not set */
+                            if(!extra)
+                                [image setTitle:nil forState:UIControlStateNormal];
+                        }
+                        else /* Remove the view if it won't work */
                             [stack removeArrangedSubview:oldview];
                     }
                     if(!image)
                     {
-                        image = [[UIImageView alloc] initWithImage:object];
+                        image = extra ? [[UIImageView alloc] init] : [UIButton buttonWithType:UIButtonTypeCustom];
 
                         [image setTranslatesAutoresizingMaskIntoConstraints:NO];
 
@@ -2425,8 +2459,12 @@
                         else
                             [stack addArrangedSubview:image];
                     }
+                    /* Set the image view or button image */
+                    if(extra)
+                        [image setImage:object];
                     else
-                        [image setImage:object];
+                        [image setImage:image forState:UIControlStateNormal];
+
                     index++;
                 }
             }