简体   繁体   中英

Xcode storyboard with dynamic views

I have a problem about Xcode storyboard and dynamically showed views.

I have 3 vertically aligned views in my storyboard and each one is linked with each other with top-bottom relationship with constraints

However during runtime I want to hide the 2nd view and replace its place with the 3'rd view (which is a tableview ) and to fill the both 2nd and 3rd places I'm trying to extend the height of tableview . However I cannot succeed. I have tried lots of things but closest thing I get is to transform the 3rd view to 2nd place but the height remains the same

My latest code is in below

    if (status) {

       self.filterView.hidden = NO;
       self.contentTable.frame = contentTableFrame;

    } else {

       self.filterView.hidden = YES;
       CGFloat predictedHeight = self.contentTable.frame.size.height+(self.contentTableFrame.origin.y-self.filterView.frame.origin.y);
    self.contentTable.transform = CGAffineTransformMakeTranslation(0,       self.filterView.frame.origin.y-self.contentTable.frame.origin.y);

       for (NSLayoutConstraint *constraint in self.contentTable.constraints) {
           if (constraint.firstAttribute == NSLayoutAttributeHeight) {
                constraint.constant = predictedHeight;
           }

       }

    }

You can also find the screenshot of the 3rd view's constraints. What should I do to fix that? Does anyone have any idea about it?

在此处输入图片说明

I also have another solution to this problem . But I've another problem on that too. When I executed the below line of code my 3rd view move to 2nd view's place but its height remains the same so on the bottom of the page it happens to seems a blank space.

        self.contentTable.transform = CGAffineTransformMakeTranslation(0,       self.filterView.frame.origin.y-self.contentTable.frame.origin.y);

In order to fix that I've tried to change its height(with .frame = CGRectMake(.....) ) but it didn't work.Then I've tried to scale the 3rd view(which is a tableview) and it succeed but because I've scaled it all the cells inside the tableview scaled too and the visual appearance of the table has broken. So I couldn't able to find a solution to that problem. It seemed like a challenge.

Thanks

you can hide filterview but you can't replace it by tableview. if you want to replace it by table view.You need to use method BringSubviewToFront for That Tableview.it will bring that tablview to front and set filterview to back which is hidden for ui.

Sorry it is in objective-c. I get the top contraint from the TableView (Storyboard). When I hide the topView, I get the height of my topView and change the top constraint constant. You can see it in @IBAction. Finally, the tableView is stretched and takes the space left from the topView. Is that what you were looking ?

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topTableViewConstraint;

- (IBAction)pushTest:(id)sender;
@end

ViewController.m

- (IBAction)pushTest:(id)sender {
    if (self.topView.hidden) {
        self.topTableViewConstraint.constant = 0;
    }else{
        self.topTableViewConstraint.constant = -self.topView.bounds.size.height;
    }
    self.topView.hidden = !self.topView.hidden;
}

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