1

enter image description hereI'm currently trying to create an iOS version of an application I previously made for Android. In the Android version, I had a LinearLayout where I could just throw in any additional layouts I needed (one of which was a RecyclerView for showing data that the user added on that particular page via a dialog), and it just worked fine. I'm now trying to replicate this on iOS with some difficulty.

Things I have tried so far: Scroll View > Stack View > dynamic Table view inside the stack view to show the user added data - so far every attempt to do this has failed - it's as if Xcode does whatever the hell it wants instead of what I'm asking it to do. Randomly assigned heights and widths in 5 figures are not uncommon. I can get content displayed, but it flat out refuses to scroll. I have made sure I have set the scrollview top, bottom leading and trailing to the superview as numerous tutorials I have seen have instructed me to do, and that the stack view also has a constraint for equal widths to the scroll view and yet still nothing.

Static Table View > dynamic Table View in one of the static cells - The issue here is that I do not want the dynamic table to be scrollable, I want this to be the height of any content in it, so if there is no content I basically want this not to exist, if there's 1 row I want it to show the 1 row, if there are 3 rows I want this to show all 3 rows without a separate scrolling action to the one in the outer table view.

Basically, I'm after one long scrollable screen, with UI elements (ideally in either a static table view or just any sort of view where I can get them to display at this point!) both above and below the dynamic table view, and the length of the dynamic table view exactly fits the content added to it. Can anything like this be done in iOS at all?

EDIT: Please find attached a picture of the Android side UI I am trying to emulate. The RecyclerView is between the third party details button and the three camera buttons.

markeh21
  • 189
  • 12
  • Can you put your UI picture? – Karim Oct 25 '17 at 11:23
  • Added a picture of the Android side I am trying to emulate. I don't have anything screenshottable to show on the iOS side yet, Interface Builder just hides everything you show and project currently not in a compilable state. – markeh21 Oct 25 '17 at 11:30

1 Answers1

0

maybe you should use UITableView,As in the comments suggested:

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return textViewCount + isCamera == true ? 1:0//
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == cameraRow {
        return cameraCell //suggest addSubview UICollectionView do this
    }else{
        return textViewCell
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == cameraRow {
        return cameraCount / 3 * cameraHeight
    }else{
        return textViewHeight
    }
}

In Objective-C,I use this calculate the size of the string:

@implementation NSString (FIDExtra)

- (CGSize)sizeWithTextMaxSize:(CGSize)textMaxSize font:(UIFont *)font
{
    CGSize textSize = CGSizeZero;

    if (self.length > 0)
    {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

        NSDictionary *attributesDic = @{NSFontAttributeName: font,
                                        NSParagraphStyleAttributeName: paragraphStyle};

        CGRect textRect = [self boundingRectWithSize:textMaxSize
                                             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                          attributes:attributesDic
                                             context:nil];

        textSize = CGSizeMake(CGRectGetWidth(textRect), ceilf(CGRectGetHeight(textRect)));
    }

    return textSize;
}

@end

I try to use swift,but I not sure useful:

extension String {
    func sizeWithTextMaxSize(textMaxSize:CGSize,font:UIFont) -> CGSize {
        var textSize = CGSize.zero
        if self.count > 0 {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineBreakMode = .byWordWrapping
            let attri = NSAttributedString.init(string: self, attributes: [NSAttributedStringKey.font:font])
            let textRect = attri.boundingRect(with: textSize, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil)
            textSize = CGSize.init(width: textRect.size.width, height: textRect.size.height)
        }
        return textSize
    }
}
Karim
  • 322
  • 4
  • 20
  • Thanks, as I'm sure this will come in handy, but the section I'm looking for help implementing is that above the camera buttons. I likely will use collectionview to do this, i'm using that in the application elsewhere already. – markeh21 Oct 25 '17 at 11:55
  • use ` optional public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat` and calculate the size of the string – Karim Oct 25 '17 at 12:10