我试图更改viewDidLayoutSubviews()中某些对象的大小和位置。方法fixPositions()被调用,但所有更改仅在进入下一个屏幕后才适用。以编程方式更改对象的大小和位置
这里是我的代码:
override func viewDidLayoutSubviews() {
let width = filterMainView.frame.width/5
filterView.frame = CGRectMake(0.0, 0.0, width, 50.0)
fixPositions(filterSubview, image: filterImage, button: filterButton, width: width)
}
func fixPositions(mainView: UIView, image: UIImageView, button: UIButton, width: CGFloat) {
let buttonFrame = CGRectMake(0.0, 0.0, width, 50.0)
button.frame = buttonFrame
mainView.frame = buttonFrame
image.frame = CGRectMake((width - 30.0)/2, 10.0, 30.0, 30.0)
}
此外,filterView做工精细,它会立即更改。这种方法有什么问题?
试着把你的代码放在'viewWillLayoutSubviews()' – AnthonyR
@AnonyRoani但是如果我应该完成这些动作5次呢?它会添加许多额外的代码行。 – Slava
我不明白你为什么使用这些委托方法。为什么不在你的元素框架被改变之后调用'self.layoutIfNeeded()'?这将更新框架。我认为你不应该为你的情况使用'layoutSubviews()'方法(因为你的UIButton框架似乎一直在改变) – AnthonyR