简体   繁体   中英

Text field is enlarged when hidding other widgets in stackview swift ios

I have added a stack view in table view cell, I have added a textField, a textView, and others views and their subviews(images labels), I have added buttons of respective widget, when I click the button, the respective widget should appear and other widgets should be hidden but the widget that appear is enlarged on whole stack view, I want the widget should occupy its original size and other size of hidden widget should be empty.

Note: I have added some UIvIEWs and textfield and Textview in a stack view, now i want to show only one view or TextField or Textview based on what Button I click then stack view should hide other widgets (like remaining views or text field or textview except what was clicked), it does fine here but the error is the selected or showed widget is enlarged in size and occupy all spcae of other widgets which are hidden

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var Cell : UITableViewCell?
        Cell = firstTableView.dequeueReusableCell(withIdentifier: "firstCell")! as UITableViewCell
        if widgetInstockArray.count == 0 && labelInstockArray.isEmpty {
            print("no textFields in instock")
        }
        else {
            print("widget process i in winstock loop")
            let i = widgetInstockArray[indexPath.row]
                if i.contains(wTextField)  {
                    print("textField was placed")
                    Cell?.contentView.viewWithTag(1)?.isHidden = true
                    Cell?.contentView.viewWithTag(2)?.isHidden = false
                    Cell?.contentView.viewWithTag(3)?.isHidden = true
                    Cell?.contentView.viewWithTag(6)?.isHidden = true
                    Cell?.contentView.viewWithTag(9)?.isHidden = true
                    Cell?.contentView.viewWithTag(12)?.isHidden = true
                    Cell?.textLabel?.text = labelInstockArray[indexPath.row]
                   }
                else {
                    print("no textField exist")
            }
            if i.contains(wCamView) {
                Cell?.contentView.viewWithTag(1)?.isHidden = true
                Cell?.contentView.viewWithTag(2)?.isHidden = true
                Cell?.contentView.viewWithTag(3)?.isHidden = false
                Cell?.contentView.viewWithTag(6)?.isHidden = true
                Cell?.contentView.viewWithTag(9)?.isHidden = true
                Cell?.contentView.viewWithTag(12)?.isHidden = true
                Cell?.textLabel?.text = labelInstockArray[indexPath.row]
            }
            else {
                print("nothing cam")
            }
            if i.contains(wTextView) {
                Cell?.contentView.viewWithTag(1)?.isHidden = false
                Cell?.contentView.viewWithTag(2)?.isHidden = true
                Cell?.contentView.viewWithTag(3)?.isHidden = true
                Cell?.contentView.viewWithTag(6)?.isHidden = true
                Cell?.contentView.viewWithTag(9)?.isHidden = true
                Cell?.contentView.viewWithTag(12)?.isHidden = true
                let tvLabel = UILabel(frame: CGRect(x: 10, y: 10, width: 200, height: 30))
                tvLabel.text = "You title"
                Cell?.contentView.viewWithTag(1)?.addSubview(tvLabel)
                Cell?.textLabel?.text =  labelInstockArray[indexPath.row]
                }
              else {
                }

            if i.contains(wCheckBox) {
                Cell?.contentView.viewWithTag(1)?.isHidden = true
                Cell?.contentView.viewWithTag(2)?.isHidden = true
                Cell?.contentView.viewWithTag(3)?.isHidden = true
                Cell?.contentView.viewWithTag(6)?.isHidden = true
                Cell?.contentView.viewWithTag(9)?.isHidden = true
                Cell?.contentView.viewWithTag(12)?.isHidden = false
                Cell?.textLabel?.text =  labelInstockArray[indexPath.row]
            }
            else {
            }
            if i.contains(wDateView) {
                Cell?.contentView.viewWithTag(1)?.isHidden = true
                Cell?.contentView.viewWithTag(2)?.isHidden = true
                Cell?.contentView.viewWithTag(3)?.isHidden = true
                Cell?.contentView.viewWithTag(6)?.isHidden = true
                Cell?.contentView.viewWithTag(9)?.isHidden = false
                Cell?.contentView.viewWithTag(12)?.isHidden = true
                Cell?.textLabel?.text =  labelInstockArray[indexPath.row]
            }
            else {
            }

            if i.contains(wSignView) {
                Cell?.contentView.viewWithTag(1)?.isHidden = true
                Cell?.contentView.viewWithTag(2)?.isHidden = true
                Cell?.contentView.viewWithTag(3)?.isHidden = true
                Cell?.contentView.viewWithTag(6)?.isHidden = false
                Cell?.contentView.viewWithTag(9)?.isHidden = true
                Cell?.contentView.viewWithTag(12)?.isHidden = true
                Cell?.textLabel?.text =  labelInstockArray[indexPath.row]
            }
            else {
            }
            print("could not add widgets")
        }

    Cell?.selectionStyle = UITableViewCellSelectionStyle.none
    return Cell!
    }

在此处输入图片说明

在此处输入图片说明

Add an empty view at the bottom that will fill the empty space if you don't have anything to display in the blank area.

As shown in below screens

在此处输入图片说明

在此处输入图片说明

Add Four UIView to UIStackView and give constraints to UIStackView as, top, left, right as 15 and Height as 100.

Then, click that StackView's Height Constraints, and Change priority to 750, then, Normal line is changed to Dotted Line [Image 1].

Image 1 :

在此处输入图片说明

Then, StackView axis should be vertical and alignment and distribution will be Fill and spacing as 10 [Image 2].

Image 2:

在此处输入图片说明

Then, now with UIView .

You have to click each and every view and set Height Constraints for this. Only height constraints should add [Image 3]?.

Image 3

在此处输入图片说明

Then, if you try to run, you will get output what you need. Try sample, then go with your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM