如何通过Switch on状态显示UIImageView?然后......如何向UIImageView添加约束以使其居中或类似?

时间:2016-11-11 17:37:57

标签: ios swift uiimageview constraints uiswitch

我正在尝试在其内部使用UISwitch创建一个UIImageView(在viewController中水平居中)。 当UIswitch转到ON位置时 - 第二个UIImageView应该出现在原始UIImageView的左侧,而约束应该使两个UIImageViews水平居中。

提前致谢

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,但最简单的方法是使用只包含一个imageView的居中水平UIStackView。当翻转开关时,通过调用insertArrangedSubview(:at:) 将第二个imageView添加到stackView,stackView将负责维护居中和任何请求的间距。同样,当开关关闭时,只需拨打removeArrangedSubview( :),一切就会恢复原状。

如果您不在iOS 9上并且没有UIStackView,您只需将IBOutlet拖到您要移动的centerX约束的视图中,然后添加到其.constant属性和动画layoutIfNeeded即可它向右滑动。

catname = []

print("Enter the name of the cats")

name = input()

if name == '':

   break