Code Monkey home page Code Monkey logo

Comments (3)

kevin-lyn avatar kevin-lyn commented on May 12, 2024

@mixdesign There is no straight forward approach for this. But I did have such requirement to adjust the popup's height according to it's subviews. Here is what I did to achieve this:

- (void)loadContentView
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width * 3 / 4;
    CGFloat height = 400;
    width = width > 300 ? width : 300;

    self.contentSizeInPopup = CGSizeMake(width, height);

    UIView *wrapperView = [UIView new]; // This is for wrappering the _contentView, to avoid constraints conflict on iOS 8.0 and 8.1

    _contentView = [UIView new];
    [wrapperView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];

    [_contentView setNeedsLayout];
    [_contentView layoutIfNeeded];

    [self contentViewDidLoad];

    [_contentView setNeedsLayout];
    [_contentView layoutIfNeeded];

    UIView *bottomView = nil;
    for (UIView *subview in _contentView.subviews) {
        if (subview.frame.origin.y + subview.frame.size.height > bottomView.frame.origin.y + bottomView.frame.size.height) {
            bottomView = subview;
        }
    }

    if (bottomView.frame.origin.y + bottomView.frame.size.height < height) {
        [_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(width, bottomView.frame.origin.y + bottomView.frame.size.height));
        }];
        [_contentView setNeedsLayout];
        [_contentView layoutIfNeeded];
    }

    self.contentSizeInPopup = _contentView.bounds.size;

    [_contentView removeFromSuperview];
}

- (void)contentViewDidLoad
{
     // For subclass overriding
}

- (void)viewDidLoad
{
    [self loadContentView];

    [super viewDidLoad];

    [self.view addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(_contentView.superview);
    }];
}

BTW, I'm using Masonry, if you are using "raw" auto constraint or other library, the idea should be the same.

from stpopup.

mixdesign avatar mixdesign commented on May 12, 2024

@kevin0571 thank you, it really helps.
Can't we somehow automate this approach?

from stpopup.

kevin-lyn avatar kevin-lyn commented on May 12, 2024

@mixdesign currently can't. Will try to figure it out, and make it as simple as possible.

from stpopup.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.