【问题标题】:Resize UITextView Parent UIView When Adding To UIScrollView添加到 UIScrollView 时调整 UITextView 父 UIView 的大小
【发布时间】:2018-06-11 11:51:06
【问题描述】:

我有一个 UIView,其中包含许多 UITextViews 调整大小以适应文本,最后一个文本视图通过 Autolayout 连接到父 UIView 的基础。然后 UIView 使用以下方法动态添加到 UIScrollView

 for (UIView *subview in self.scrollViewContent.subviews) {
        [subview removeFromSuperview];
    }

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;

    NSLog(@"%f",self.txtViewEndOfFAQ.frame.size.height);

    [self.viewFAQ setFrame:CGRectMake(0, 0, screenWidth, (self.txtViewEndOfFAQ.frame.origin.y + self.txtViewEndOfFAQ.frame.size.height))];


    [self.scrollViewContent addSubview:self.viewFAQ];
    [self.scrollViewContent setContentSize:CGSizeMake(self.viewFAQ.frame.size.width,   self.viewFAQ.frame.size.height)];


    [self addUnderline:self.imageViewTitle4];

    [self showMenu:nil];

现在的问题是,当它加载时它没有给我正确的高度。对于较窄的设备,文本视图的高度会增加以适应文本,这似乎没有被考虑在内。

【问题讨论】:

  • 为什么不简单地使用 UITableView。会容易很多
  • 我同意@rv7284。使用带有自动行高计算的表格视图。它应该很简单,iOS 会自动为你管理这些事情。
  • 你在哪里运行这段代码?是否在 viewDidLoad 中?
  • 不,稍后按下 UIButton,我想我会尝试切换到 UITableView 看看会发生什么。

标签: ios objective-c


【解决方案1】:

只要不与现有约束产生冲突,您就可以通过编程方式添加高度约束。使用 NSLayoutConstraint 会是这样的:

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint
                       constraintWithItem:self.viewFAQ
                       attribute:NSLayoutAttributeHeight
                       relatedBy:NSLayoutRelationEqual
                       toItem:self.txtViewEndOfFAQ
                       attribute:NSLayoutAttributeNotAnAttribute
                       multiplier:1.0
                       constant:0];

[self addConstraints: heightConstraint];

使用 NSLayoutAnchor:

[self.viewFAQ.heightAnchor constraintEqualToAnchor:self.txtViewEndOfFAQ.heightAnchor].active = YES;

【讨论】:

    猜你喜欢
    • 2020-07-20
    • 2012-11-24
    • 2023-03-11
    • 2019-06-25
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多