- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 View Controller 中,我有一个用于托 pipe View 的 View 。我们称之为ViewA
。当 Controller 加载时,从 nib 加载的 View 将被设置为 ViewA 内部的 subview 。根据 subview 中的内容,其高度可以是不同的大小。
因此,我创建了一个委托(delegate),它会在 subview 的高度发生变化时发出警报,通知其父 View 更新自己的高度:
UIViewController
class MyViewController: UIViewController, MyViewDelegate {
@IBOutlet weak var myView: UIView!
@IBOutlet weak var myViewHeightConstraint: NSLayoutConstraint!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let mySubView: MySubView = UINib(nibName: "MySubView", bundle: nil).instantiate(withOwner: MySubView(), options: nil)[0] as! MySubView
mySubview.translatesAutoresizingMaskIntoConstraints = false
mySubView.delegate = self
myView.addSubView(mySubView)
let leadingConstraint = NSLayoutConstraint(item: mySubView, attribute: .leading, relatedBy: .equal, toItem: myView, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: mySubView, attribute: .trailing, relatedBy: .equal, toItem: myView, attribute: .trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: mySubView, attribute: .top, relatedBy: .equal, toItem: myView, attribute: .top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: mySubView, attribute: .bottom, relatedBy: .equal, toItem: myView, attribute: .bottom, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])
}
override func viewDidLayoutSubviews() {
addShadowToView()
}
func addShadowToView() {
myView.layer.masksToBounds = false
myView.layer.shadowColor = UIColor.black.cgColor
myView.layer.shadowOpacity = 0.25
myView.layer.shadowOffset = CGSize(width: 0, height: 0)
myView.layer.shadowRadius = 5.0
myView.layer.shadowPath = UIBezierPath(rect. myView.bounds).cgPath
}
MySubViewDelegate(_ mySubView: MySubView, didUpdateHeightTo height: CGFloat) {
myViewHeightConstraint.constant = height
myView.updateConstraints()
addShadowToView()
}
}
MySubView
class MySubView: UIView {
var delegate: MySubViewDelegate?
@IBOutlet weak var aView: UIView!
@IBOutlet weak var aViewHeghtConstraint: NSLayoutConstraint!\
var isViewCollapsed = false
@IBAction func toggleView() {
aViewHeightConstraint.contant = isViewCollapsed ? 100 : 0
isViewCollapsed = !isViewCollapsed
updateConstraints()
delegate.MSView(self, didUpdateHeightTo height: self.frame.height)
}
}
protocol MySubViewDelegate {
func MSView(_ MySubView: MySubView, didUpdateHeightTo height: CGFloat)
}
是否有更好的方法将可展开和折叠的 subview 放入父 View 中,以便能够更新其自己的框架以适应其 subview 的更改?
最佳答案
发表评论并发布代码后...
看起来您正在做很多不需要做的事情。
有了适当的约束,您需要的代码就会少得多,并且根本不需要协议(protocol)/委托(delegate)。
首先 - 这只是一个提示 - 您可以以更简单、更易读的方式在代码中定义约束:
myView.addSubview(mySubView)
NSLayoutConstraint.activate([
mySubView.topAnchor.constraint(equalTo: myView.topAnchor, constant: 0.0),
mySubView.bottomAnchor.constraint(equalTo: myView.bottomAnchor, constant: 0.0),
mySubView.leadingAnchor.constraint(equalTo: myView.leadingAnchor, constant: 0.0),
mySubView.trailingAnchor.constraint(equalTo: myView.trailingAnchor, constant: 0.0),
])
第二 - 也是一个提示 - 如果您创建一个“阴影” View 子类,它可以自行处理您的阴影更新:
class MyShadowedView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
// non-changing properties - set on init
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.25
layer.shadowOffset = CGSize(width: 0, height: 0)
layer.shadowRadius = 5.0
}
override func layoutSubviews() {
super.layoutSubviews()
// update the shadowPath
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
}
}
当您将 myView
作为 UIView
添加到 Storyboard 时,只需将其类分配给 MyShadowedView
即可,无需创建任何添加阴影的调用 - 它都会自行完成。
第三 - 根据您发布的代码,您的 myView
似乎有高度限制,并且您将 mySubView
限制在其顶部和底部。这意味着 mySubView
将是 myView
的高度,并且永远不会改变。您的“委托(delegate)”函数尝试更改它,但它总是超过其约束高度。
所以...
在你的ViewController中,你想要添加一个UIView
,将其类分配给MyShadowedView
,像平常一样给它顶部、前导和尾随约束(或者顶部、宽度和 CenterX(如果您需要的话)。
对于它的高度,给它一个关于它应该开始的高度约束,但是将该约束设置为将在运行时删除的占位符。这允许您在设计期间看到它(并避免 IB 提示缺少约束),但在运行时您添加的 subview 将控制其高度:
你的 xib 看起来像这样(嗯,简化的 - 我确信你里面有更多的元素):
注意:为 aView
的底部约束设置 999
优先级也有助于避免 IB 约束警告。
当您点击按钮时,您的代码将在 100
和 0
之间切换 aView
的高度约束常量。这将扩展/折叠其 super View 的高度,这将控制其 super View ( View Controller 中的myView
)的高度。
您的完整代码最终为:
//
// TannerViewController.swift
//
// Created by Don Mag on 12/12/18.
//
import UIKit
class MySubView: UIView {
@IBOutlet weak var aView: UIView!
@IBOutlet weak var aViewHeightConstraint: NSLayoutConstraint!
var isViewCollapsed = false
@IBAction func toggleView(_ sender: Any) {
aViewHeightConstraint.constant = isViewCollapsed ? 100 : 0
isViewCollapsed = !isViewCollapsed
}
}
class TannerViewController: UIViewController {
@IBOutlet weak var myView: MyShadowedView!
override func viewDidLoad() {
super.viewDidLoad()
let mySubView: MySubView = UINib(nibName: "MySubView", bundle: nil).instantiate(withOwner: MySubView(), options: nil)[0] as! MySubView
mySubView.translatesAutoresizingMaskIntoConstraints = false
myView.addSubview(mySubView)
NSLayoutConstraint.activate([
mySubView.topAnchor.constraint(equalTo: myView.topAnchor, constant: 0.0),
mySubView.bottomAnchor.constraint(equalTo: myView.bottomAnchor, constant: 0.0),
mySubView.leadingAnchor.constraint(equalTo: myView.leadingAnchor, constant: 0.0),
mySubView.trailingAnchor.constraint(equalTo: myView.trailingAnchor, constant: 0.0),
])
}
}
class MyShadowedView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
// non-changing properties - set on init
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.25
layer.shadowOffset = CGSize(width: 0, height: 0)
layer.shadowRadius = 5.0
}
override func layoutSubviews() {
super.layoutSubviews()
// update the shadowPath
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
}
}
结果:
<小时/>为了轻松测试这一点,这里是 Storyboard的源代码:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="jPc-3G-hfP">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Tanner View Controller-->
<scene sceneID="f8L-af-3cE">
<objects>
<viewController id="jPc-3G-hfP" customClass="TannerViewController" customModule="SW4Temp" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="0I2-oK-Mx2">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view clipsSubviews="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="uKj-M5-owl" customClass="MyShadowedView" customModule="SW4Temp" customModuleProvider="target">
<rect key="frame" x="40" y="120" width="295" height="100"/>
<color key="backgroundColor" red="1" green="0.83234566450000003" blue="0.47320586440000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="100" placeholder="YES" id="qvT-aM-Weq" userLabel="Placeholder Height = 100"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" red="0.99953407049999998" green="0.98835557699999999" blue="0.47265523669999998" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="uKj-M5-owl" firstAttribute="top" secondItem="twx-NV-wpY" secondAttribute="top" constant="100" id="141-8C-ZNl"/>
<constraint firstItem="twx-NV-wpY" firstAttribute="trailing" secondItem="uKj-M5-owl" secondAttribute="trailing" constant="40" id="5Zs-Or-GhR"/>
<constraint firstItem="uKj-M5-owl" firstAttribute="leading" secondItem="twx-NV-wpY" secondAttribute="leading" constant="40" id="R95-i1-Xb2"/>
</constraints>
<viewLayoutGuide key="safeArea" id="twx-NV-wpY"/>
</view>
<connections>
<outlet property="myView" destination="uKj-M5-owl" id="uCY-bV-QJd"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="q6f-6s-ke9" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53.600000000000001" y="101.19940029985008"/>
</scene>
</scenes>
</document>
<小时/>
和 mySubView.xib:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="MySubView" customModule="SW4Temp" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="332" height="202"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="1000" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="T7q-1U-5iM">
<rect key="frame" x="106" y="20" width="120" height="30"/>
<color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" constant="120" id="WJn-fl-dH1"/>
</constraints>
<state key="normal" title="Button"/>
<connections>
<action selector="toggleView:" destination="iN0-l3-epB" eventType="touchUpInside" id="LSR-3h-g1f"/>
</connections>
</button>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Vtd-O9-gRZ">
<rect key="frame" x="40" y="70" width="252" height="100"/>
<color key="backgroundColor" red="0.46202266219999999" green="0.83828371759999998" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="100" id="e3B-MV-NZK"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" red="0.83216959239999999" green="0.98548370600000001" blue="0.47333085539999997" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="Vtd-O9-gRZ" secondAttribute="bottom" priority="999" constant="20" id="0aE-RM-0AZ"/>
<constraint firstItem="T7q-1U-5iM" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="20" id="Acg-yV-bn2"/>
<constraint firstItem="Vtd-O9-gRZ" firstAttribute="top" secondItem="T7q-1U-5iM" secondAttribute="bottom" constant="20" id="KVh-lw-Sst"/>
<constraint firstItem="T7q-1U-5iM" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="NUj-4y-fDg"/>
<constraint firstItem="Vtd-O9-gRZ" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="40" id="cS4-7R-wW7"/>
<constraint firstAttribute="trailing" secondItem="Vtd-O9-gRZ" secondAttribute="trailing" constant="40" id="kkG-9K-cEP"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="aView" destination="Vtd-O9-gRZ" id="SNl-ng-33p"/>
<outlet property="aViewHeightConstraint" destination="e3B-MV-NZK" id="S4R-ct-gzE"/>
</connections>
<point key="canvasLocation" x="12" y="-179"/>
</view>
</objects>
</document>
关于ios - 处理包含能够折叠和展开的 subview 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53732414/
我在自定义表格单元格中长按时添加 subview ,但是当我滚动表格时, subview 消失。 p> 我应该怎么做,以便当我向后滚动时,该 subview 仍然显示/出现在该单元格上,或者换句话说,
如何遍历 UIView 的所有 subview ,以及它们的 subview 和它们的 subview ? 最佳答案 使用递归: // UIView+HierarchyLogging.h @inter
如果我有 contentView 和 2 个 subview 。我想根据subviewB居中subviewA subviewA 和 subviewB 有相同的父 View 。 这就是我现在正在做的,将
我想向 self.view 添加一个覆盖整个区域的 subview (如调光 View )。但在它下面,有一些 View 我不希望受到添加的 subview 的影响(我不希望这些 View 具有变暗的
我正在尝试向 UITabBar 添加 subview ,它应该位于其他 UITabBar subview 之后。 我在 UITabBarController 的子类中添加了这样的 subview :
图像描述了我必须将这种 subview 添加到我现有的单元格中,并且在多次单击“添加”图标时还要添加相同的 subview 。 我在添加 subview 时遇到困难。如果有人能为我提供处理此结构的正确
我添加了一个 subview ,如下所示: func showPlayerView2() { self.view.frame = CGRect(x: 0, y: 0, width: view.
我的问题是关于 UIView/CALayer: Transform triggers layoutSubviews in superview 据报道,来自苹果的 TSI 回复: Generally,
我试图为我的主视图创建一个辅助 View 。这个 subview 需要很小的高度,它需要适合另一个带有标签的大 UIView。 问题是当我使用 UIView addSubview 时,第三个 UIVi
我正在尝试淡出整个 View ,但只有一个特定的 subview , 这将强调 subview更清晰的外观。假设 self.view 有 5 textfields和 2 UIView如subviews
我确信我在这里错过了一些愚蠢的东西,但我有 mainView、subviewA 和 subviewB。我试图将 subviewB 添加到 subviewA 并将其锚定在 subviewA 内部,但是它
我有一个包含 3 个 subview 的 View ,分别称为 view A、view B、view C。 当我删除 subview 时,意味着我需要自动设置边距以进行调整。 当我删除 view A
我有两个加载的 subview 。一个是位于 View Controller 内部的选项卡栏,它很早就加载,第二个是按下选项卡栏项目时出现的 View 。 但是,当添加此 subview 时,它会加载
在 iOS 应用中,我在主视图中添加了一个 subview : [self.view addSubview:firstUIImageSubview]; 但是在 subview 类 firstUIIma
我正在制作一个球程序,您可以通过长按手势将球 UIView subview 添加到 super View 。点击 View 会将其从父 View 中移除。 添加所有 View 效果很好,重新添加 Vi
我使用以下代码在单击按钮时将日期选择器 View 添加到我的应用程序主视图。 - (IBAction)CalenderButton_Click:(id)sender { // code to add
我正在努力向 View 添加 subview 。进度变量在其 View Controller 的 viewDidLoad() 中设置。 progressView frame size 设置正确,只是添
这是我的代码 var button = [UIButton?](count:3, repeatedValue: UIButton()) for i in 0...2{
我有一个 UIViewController,里面有几个不同的 UIView subview 。当用户调用某个 Action 时,我手动更改每个的大小、比例和位置。我想更新其中一个 UILabel( s
我的屏幕有一个主视图模型。它由 2 个 subview 模型组成。 其中一个负责注册部分。其中一个处理登录部分。其中一个负责处理菜单部分(如果经过身份验证,可以显示哪些菜单项,以及“欢迎“用户名”类型
我是一名优秀的程序员,十分优秀!