在UIstackview swift中删除视图

时间:2017-01-05 11:16:21

标签: ios user-interface uistackview

我是swift的新手。我已使用addArrangedSubview()向stackview添加了视图。但我无法使用removeArrangedSubview()删除此视图。 即使在删除已安排的子视图后,视图仍然存在

import Foundation
import UIKit

class render: UIViewController {

  let subview    = UIStackView()
  let mainview   = UIStackView()

  override func viewDidLoad() {
    super.viewDidLoad()

    self.mainviewlet()
    self.login()
  }

  func login() {

    let username = UITextField()
    // text field

    let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
    //button

    // Adding to subview  
    subview.axis  = UILayoutConstraintAxis.vertical
    subview.distribution  = UIStackViewDistribution.equalSpacing
    subview.alignment = UIStackViewAlignment.center
    subview.spacing   = 16.0

    subview.addArrangedSubview(username)
    subview.addArrangedSubview(button)

    subview.translatesAutoresizingMaskIntoConstraints = false;

    // Adding subview to another stackview
    mainview.addArrangedSubview(subview)
    self.view.addSubview(mainview)

}

在另一个功能中,我删除了已安排的子视图

func signup(sender: UIButton!) {

    // Remove subview
    mainview.removeArrangedSubview(subview)
    subview.removeFromSuperview()

    let firstname = UITextField()
    firstname.textAlignment = NSTextAlignment.center
    firstname.textColor = UIColor.black
    firstname.frame = CGRect()
    firstname.frame.size.height = 30;
    firstname.text = "firstname"

    subview.addArrangedSubview(firstname)
    mainview.addArrangedSubview(subview)
    self.view.addSubview(mainview)
}

我的主视图创建为:

func mainviewlet {

  mainview.axis  = UILayoutConstraintAxis.vertical
  mainview.distribution  = UIStackViewDistribution.equalSpacing
  mainview.alignment = UIStackViewAlignment.center
  mainview.spacing   = 16.0
  mainview.translatesAutoresizingMaskIntoConstraints = false;

  self.view.addSubview(mainview)
}

我想要username&要删除button并将新字段firstname添加到子视图中。

我是以正确的方式做到的吗?如何删除子视图?谢谢你的帮助

5 个答案:

答案 0 :(得分:17)

如果要在堆栈视图中隐藏视图,您只需将包含视图的hidden属性设置为true,堆栈视图将处理其余视图。

所以,根据我的代码,您必须致电以下内容:

//class fields
Vector3 AnimatedSpeed;

Vector3 AnimationDelta;

//basic calculation

//get the direction vector from the players current position to the future 
block position

Vector3 dirVector = futurePos - transform.position;
//find the rotation from the current orientation to the direction vector
Quaternion rotation = Quaternion.FromToRotation(transform.forward, dirVector);

//calculate the distance from you to the cube and scale it with the magnitude of the AnimationDelta
float result = Vector3.Distance(transform.position, futurePos);

result = result / animationDelta.magnitude;

//finally rotate the forward vector by the rotation and multiply it by the 
//animation speed and the result to get the step by step movement as
//the animation plays. NOTE: The animation should be based on forward direction

transform.position += (AnimationSpeed * rotation) * result * Time.deltaTime;

答案 1 :(得分:6)

在Swift 4.2中

removeArrangedSubview方法从堆栈的splittedSubviews数组中删除提供的视图。该视图的位置和大小将不再由堆栈视图管理。但是,此方法不会从堆栈的子视图数组中删除提供的视图;因此,该视图仍显示为视图层次结构的一部分。

为防止在调用堆栈的removeArrangedSubview:方法后视图出现在屏幕上,请通过调用视图的removeFromSuperview()方法将其从子视图数组中显式删除,或将视图的isHidden属性设置为true。

所以:

myStackView.removeArrangedSubview(myView)
myView.removeFromSuperview()

答案 2 :(得分:0)

您走在正确的轨道上,但您的代码存在一些问题。

首先,您应该只在self.view.addSubview(mainview)中拨打mainviewlet一次。您不应该在loginsignup中再次执行此操作。

编辑:删除了第二条评论,因为它不正确。

第三,您应该平衡对addArrangedSubviewremoveArrangedSubview的来电。您正在向username添加buttonsubview,但从不删除它们。如果你想让它们消失,你需要删除它们。

答案 3 :(得分:0)

根据我的经验,通过实际测试实现(?),从stackView中删除子视图所需要做的就是在stackView的每个子视图上调用removeFromSuperview()方法:

stackView.subviews.forEach { (view) in
    view.removeFromSuperview()
}

这是它在控制台中的外观:

(lldb) po stackView.arrangedSubviews
▿ 3 elements
  - 0 : <UIStackView: 0x7f840c8297d0; frame = (0 0; 375 95); layer = <CATransformLayer: 0x600003f48c80>>
  - 1 : <UIStackView: 0x7f840c82f5e0; frame = (0 125; 375 95); layer = <CATransformLayer: 0x600003f54520>>
  ▿ 2 : <UIView: 0x7f840b3c93a0; frame = (0 250; 375 47); opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x600006bdae20>; layer = <CALayer: 0x600003f4a7e0>>

(lldb) po stackView.subviews
▿ 3 elements
  - 0 : <UIStackView: 0x7f840c8297d0; frame = (0 0; 375 95); layer = <CATransformLayer: 0x600003f48c80>>
  - 1 : <UIStackView: 0x7f840c82f5e0; frame = (0 125; 375 95); layer = <CATransformLayer: 0x600003f54520>>
  ▿ 2 : <UIView: 0x7f840b3c93a0; frame = (0 250; 375 47); opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x600006bdae20>; layer = <CALayer: 0x600003f4a7e0>>

然后您删除所有子视图:

(lldb) expression stackView.subviews.forEach { (view) in view.removeFromSuperview() }

结果:

(lldb) po stackView.subviews
0 elements

(lldb) po stackView.arrangedSubviews
0 elements

您可以扩展UIStackView来节省将来的输入:

extension UIStackView {
    func removeAllSubviews() {
        subviews.forEach { (view) in
            view.removeFromSuperview()
        }
    }
}

答案 4 :(得分:0)

如果要删除视图而不再次引用它:

  1. 确保视图通过插座连接
  2. fooView.removeFromSuperview()

将在删除fooView的情况下绘制stackView