swift 如何以编程方式更新视图的高度?

djmepvbi  于 2023-05-16  发布在  Swift
关注(0)|答案(5)|浏览(347)

我面临的一个问题与autolayouts有关。我设置高度的看法包含图像意见为零第一次通过自动布局。但是如果调用了某个函数,我希望将高度更新为常量值,但视图的高度没有更新。下面是代码,我已经在函数中以编程方式更新了高度,但它不起作用。

let heightContraint = NSLayoutConstraint(item: businessImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 40)
businessImageView.addConstraint(heightContraint)
biswetbf

biswetbf1#

首先创建高度约束的IBOutlet。
您只需要更改约束的constant属性。例如:

self.consTblFilterHeight.constant = 100.0
self.view.layoutIfNeeded()

self.view替换为要更改高度的视图的父视图。

ev7lccsx

ev7lccsx2#

创建约束出口,然后按如下方式设置:

  • self.heightConstraintOutlet.constant = newHeightValue
tkqqtvp1

tkqqtvp13#

businessImageView.addConstraint(heightContraint)不是更新约束的代码。它添加了约束。
为了更新父视图(包含图像)的高度,您需要更新businessImageView的height约束的常量。

businessImageView.heightConstraint.constant = 40
self.view.layoutIfNeeded()
xmd2e60i

xmd2e60i4#

方法

  • 激活约束
  • 改变常数值

代码

heightConstraint.isActive = true 
heightConstraint.constant = 20
xdnvmnnf

xdnvmnnf5#

//让我们检测屏幕的高度

int height = [UIScreen mainScreen].bounds.size.height;
    int width = [UIS

[bounds.size.width;

// share the news

 NSString *message = [[NSString alloc]initWithFormat:@"This screen is \n\n%i pixels high and\n%i pixels wide", height, width];
    NSLog(@"%@", message);

相关问题