- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果有人能在这里指导我找到正确的解决方案,那就太棒了。这是我今天在这里的第二个问题,我很犹豫是否在这里问它,但我正在学习全新的东西(比如创建你自己的分段控件等),并且实际上不知道从哪里开始这个错误。我尝试用我有限的知识尽可能多地进行调试。
这是我的问题摘要。
我有两种看法。内部 View 的宽度取决于外部 View 的宽度。因此,如果我更新外部 View 的宽度约束(使用约束 IBoutlet),外部 View 的宽度会发生变化,但内部 View 的宽度仍与旧的相同。在更改外部 View 的宽度约束后,我在外部 View 上执行了layoutIfNeeded(),但没有任何反应。
详细信息:
我有一个分段控件(外部 View ),其选择器的宽度(内部 View )取决于分段控件的总宽度。正如我上面所说,在我更改分段控件的总宽度后,选择器的宽度保持不变。选择器的宽度取决于分段控件的宽度。
这是说明我的问题的图片。
正如您所看到的,我的选择器的宽度没有更新。它应该是新分段控件总宽度的一半。
我如何更新分段控件的宽度?
基本上,我将分段控件的宽度约束作为 VC 中的 IBOUtlet,然后根据屏幕尺寸增加了它的宽度。但选择器的宽度保持不变。
这是我用来更改分段控件宽度的代码
DispatchQueue.main.async {
self.segmentedWidthControl.constant = UIScreen.main.bounds.width/2
self.segmentedControl.layoutIfNeeded()
//self.segmentedControl.updateConstraints() // this doesn't work either
}
对于自定义分段控件,我遵循了 youtube 上的教程。这是代码
@IBDesignable
class SegmentedControl: UIControl{
var buttons = [UIButton]()
var selector: UIView!
var selectSegmentIndex = 0
@IBInspectable
var borderWidth: CGFloat = 0{
didSet{
layer.borderWidth = borderWidth
}
}
@IBInspectable
var borderColor: UIColor = .clear {
didSet{
layer.borderColor = borderColor.cgColor
}
}
override func draw(_ rect: CGRect) {
layer.cornerRadius = frame.height/2
}
@IBInspectable
var commaSeperatedButtonTitles: String = ""{
didSet{
updateView()
}
}
@IBInspectable
var selectorColor: UIColor = .white{
didSet{
updateView()
}
}
@IBInspectable
var selectorTextColor: UIColor = .white{
didSet{
updateView()
}
}
@IBInspectable
var TextColor: UIColor = .lightGray {
didSet{
updateView()
}
}
func updateView(){
buttons.removeAll()
subviews.forEach { $0.removeFromSuperview()}
let buttonTitles = commaSeperatedButtonTitles.components(separatedBy: ",")
for buttonTitle in buttonTitles{
let button = UIButton(type: .system)
button.setTitle(buttonTitle, for: .normal)
button.setTitleColor(TextColor, for: .normal)
button.addTarget(self, action: #selector(buttonTapped(button: )), for: .touchUpInside )
buttons.append(button)
}
buttons[0].setTitleColor(selectorTextColor, for: .normal)
let selectorWidth = frame.width/CGFloat(buttonTitles.count)
selector = UIView(frame: CGRect(x: 0, y: 0, width: selectorWidth, height: frame.height))
selector.backgroundColor = selectorColor
selector.translatesAutoresizingMaskIntoConstraints = false
selector.layer.cornerRadius = frame.height/2
addSubview(selector)
let sv = UIStackView(arrangedSubviews: buttons)
sv.axis = .horizontal
sv.alignment = .fill
sv.translatesAutoresizingMaskIntoConstraints = false
sv.distribution = .fillEqually
addSubview(sv)
sv.topAnchor.constraint(equalTo: topAnchor).isActive = true
sv.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
sv.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
sv.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
}
@objc func buttonTapped(button:UIButton){
for (buttonIndex,btn) in buttons.enumerated(){
btn.setTitleColor(TextColor, for: .normal)
if(btn == button){
selectSegmentIndex = buttonIndex
let selectorStartPosition = frame.width/CGFloat(buttons.count) * CGFloat(buttonIndex)
UIView.animate(withDuration: 0.3) {
self.selector.frame.origin.x = selectorStartPosition
}
btn.setTitleColor(selectorTextColor, for: .normal)
}
}
sendActions(for: .valueChanged)
}
}
如果您想运行该应用程序,请点击以下 GITHUB 链接。 https://github.com/Rikenm/Auto-Counter-iOS
最后感谢您的帮助。
最佳答案
我强烈建议使用约束和自动布局,而不是显式设置框架。
这是您的自定义类,实际上只做了一些更改。我已经评论了我所做的一切:
@IBDesignable
class SegmentedControl: UIControl{
var buttons = [UIButton]()
var selector: UIView!
var selectSegmentIndex = 0
// leading constraint for selector view
var selectorLeadingConstraint: NSLayoutConstraint!
@IBInspectable
var borderWidth: CGFloat = 0{
didSet{
layer.borderWidth = borderWidth
}
}
@IBInspectable
var borderColor: UIColor = .clear {
didSet{
layer.borderColor = borderColor.cgColor
}
}
override func draw(_ rect: CGRect) {
layer.cornerRadius = frame.height/2
}
@IBInspectable
var commaSeperatedButtonTitles: String = ""{
didSet{
updateView()
}
}
@IBInspectable
var selectorColor: UIColor = .white{
didSet{
updateView()
}
}
@IBInspectable
var selectorTextColor: UIColor = .white{
didSet{
updateView()
}
}
@IBInspectable
var TextColor: UIColor = .lightGray {
didSet{
updateView()
}
}
// this will update the control in IB
// when constraints are changed
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
updateView()
}
// this will keep the selector corners "round"
override func layoutSubviews() {
super.layoutSubviews()
selector.layer.cornerRadius = selector.frame.height / 2.0
}
func updateView(){
buttons.removeAll()
subviews.forEach { $0.removeFromSuperview()}
let buttonTitles = commaSeperatedButtonTitles.components(separatedBy: ",")
for buttonTitle in buttonTitles{
let button = UIButton(type: .system)
button.setTitle(buttonTitle, for: .normal)
button.setTitleColor(TextColor, for: .normal)
button.addTarget(self, action: #selector(buttonTapped(button: )), for: .touchUpInside )
buttons.append(button)
}
buttons[0].setTitleColor(selectorTextColor, for: .normal)
// not needed
//let selectorWidth = frame.width/CGFloat(buttonTitles.count)
// we're going to use auto-layout, so no need to set a frame
//selector = UIView(frame: CGRect(x: 0, y: 0, width: selectorWidth, height: frame.height))
selector = UIView(frame: CGRect.zero)
selector.backgroundColor = selectorColor
selector.translatesAutoresizingMaskIntoConstraints = false
selector.layer.cornerRadius = frame.height/2
addSubview(selector)
// constrain selector top to self.top
selector.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
// constrain selector height to self.height
selector.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
// constrain selector width to self.width
// with multiplier of 1 / number of buttons
let m = 1.0 / CGFloat(buttons.count)
selector.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: m).isActive = true
// instantiate leading constraint for selector, and
// keep a reference in var selectorLeadingConstraint
selectorLeadingConstraint = selector.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0.0)
// make it active
selectorLeadingConstraint.isActive = true
let sv = UIStackView(arrangedSubviews: buttons)
sv.axis = .horizontal
sv.alignment = .fill
// sv.distribution = .fillProportionally
sv.translatesAutoresizingMaskIntoConstraints = false
sv.distribution = .fillEqually
addSubview(sv)
sv.topAnchor.constraint(equalTo: topAnchor).isActive = true
sv.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
sv.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
sv.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
}
@objc func buttonTapped(button:UIButton){
for (buttonIndex,btn) in buttons.enumerated(){
btn.setTitleColor(TextColor, for: .normal)
if(btn == button){
selectSegmentIndex = buttonIndex
let selectorStartPosition = frame.width/CGFloat(buttons.count) * CGFloat(buttonIndex)
// update selector's leading constraint, instead of explicit frame
//self.selector.frame.origin.x = selectorStartPosition
self.selectorLeadingConstraint.constant = selectorStartPosition
UIView.animate(withDuration: 0.3) {
self.layoutIfNeeded()
}
btn.setTitleColor(selectorTextColor, for: .normal)
}
}
sendActions(for: .valueChanged)
}
}
<小时/>
编辑:
这是另一种选择。不使用 UIStackView,而是使用约束来布局按钮。 “选择器” View 现在将成为按钮的同级 View ,因此您可以使用 centerX
约束,而不是计算 .leadingAnchor
约束。
最大的好处是,现在您可以在显示自定义分段控件后更改其大小,并且“选择器”大小和位置将自动更新。例如,如果将控件的宽度设置为屏幕(或其 super View )宽度的 50%,然后旋转设备以使控件变宽或变窄。
@IBDesignable
class SegmentedControl: UIControl{
var buttons = [UIButton]()
var selector: UIView!
// centerX constraint for selector view
var selectorCenterXConstraint: NSLayoutConstraint!
@IBInspectable
var borderWidth: CGFloat = 0{
didSet{
layer.borderWidth = borderWidth
}
}
@IBInspectable
var borderColor: UIColor = .clear {
didSet{
layer.borderColor = borderColor.cgColor
}
}
override func draw(_ rect: CGRect) {
layer.cornerRadius = frame.height/2
}
@IBInspectable
var commaSeperatedButtonTitles: String = ""{
didSet{
updateView()
}
}
@IBInspectable
var selectorColor: UIColor = .white{
didSet{
updateView()
}
}
@IBInspectable
var selectorTextColor: UIColor = .white{
didSet{
updateView()
}
}
@IBInspectable
var TextColor: UIColor = .lightGray {
didSet{
updateView()
}
}
// this will update the control in IB
// when constraints are changed
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
updateView()
}
// this will keep the selector corners "round"
override func layoutSubviews() {
super.layoutSubviews()
selector.layer.cornerRadius = selector.frame.height / 2.0
}
func updateView(){
buttons.removeAll()
subviews.forEach { $0.removeFromSuperview()}
// deactivate centerX constraint if its been initialized
if selectorCenterXConstraint != nil {
selectorCenterXConstraint.isActive = false
}
// add the selector view first
selector = UIView(frame: CGRect.zero)
selector.backgroundColor = selectorColor
selector.translatesAutoresizingMaskIntoConstraints = false
selector.layer.cornerRadius = frame.height/2
addSubview(selector)
let buttonTitles = commaSeperatedButtonTitles.components(separatedBy: ",")
for buttonTitle in buttonTitles{
let button = UIButton(type: .system)
button.setTitle(buttonTitle, for: .normal)
button.setTitleColor(TextColor, for: .normal)
button.addTarget(self, action: #selector(buttonTapped(button: )), for: .touchUpInside )
buttons.append(button)
}
buttons[0].setTitleColor(selectorTextColor, for: .normal)
// add each button and set top and height constraints
buttons.forEach {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
$0.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
}
// constrain first button's leading to self.leading
// constrain last button's trailing to self.trailing
NSLayoutConstraint.activate([
buttons[0].leadingAnchor.constraint(equalTo: self.leadingAnchor),
buttons[buttons.count - 1].trailingAnchor.constraint(equalTo: self.trailingAnchor),
])
// constrain each button's width to the first button's width
for i in 1..<buttons.count {
buttons[i].leadingAnchor.constraint(equalTo: buttons[i - 1].trailingAnchor).isActive = true
buttons[i].widthAnchor.constraint(equalTo: buttons[0].widthAnchor).isActive = true
}
// constrain selector top, height and width to first button's top, height and width
selector.topAnchor.constraint(equalTo: buttons[0].topAnchor).isActive = true
selector.heightAnchor.constraint(equalTo: buttons[0].heightAnchor).isActive = true
selector.widthAnchor.constraint(equalTo: buttons[0].widthAnchor).isActive = true
// constrain selector's centerX to first button's centerX
selectorCenterXConstraint = selector.centerXAnchor.constraint(equalTo: buttons[0].centerXAnchor)
selectorCenterXConstraint.isActive = true
}
@objc func buttonTapped(button:UIButton){
buttons.forEach { btn in
btn.setTitleColor(TextColor, for: .normal)
if (btn == button) {
// deactivate selector's current centerX constraint
self.selectorCenterXConstraint.isActive = false
// constrain selector's centerX to selected button's centerX
self.selectorCenterXConstraint = self.selector.centerXAnchor.constraint(equalTo: btn.centerXAnchor)
// re-activate selector's centerX constraint
self.selectorCenterXConstraint.isActive = true
UIView.animate(withDuration: 0.3) {
self.layoutIfNeeded()
}
btn.setTitleColor(selectorTextColor, for: .normal)
}
}
sendActions(for: .valueChanged)
}
}
<小时/>
在这里发布问题的一个技巧 --- 阅读 How to create a Minimal, Complete, and Verifiable Example
关于ios - 更改内部 View 的宽度,其中内部 View 的宽度取决于外部 View 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52998870/
我的理解是 width: 100% 让元素的宽度与其父元素的宽度相同,而 width: inherit 只有在明确指定父元素的宽度时才这样做.这种理解是否正确? 如果是这样,在我看来,当 width:
并设置“高度”为全屏的 1/2。 这是我的代码: div{ background:red; } 最佳答案 我会结合使用 css 和 javascript(使
编辑 2: 问题似乎出在规则的“bigTable”元素上。显然,在布局模板上使用时继承了错误的最小宽度。我仍在调查此事。 不过,我将再尝试一次 div。一个大问题是使用固定导航和动态内容,但我已经为此
我的网站需要显示宽表。在它上面是标题,它应该和整个页面一样宽(在这种情况下,和表格一样宽)。但是,它的宽度与视口(viewport)(屏幕尺寸)一样宽,因此显示时看起来还不错,但是一旦用户滚动到侧面,
我有一个小问题。我总是使用 float 来安排我的元素。我正在转向 flexbox,我做了一些例子,一切都很好,但我正在做一个事情进展不顺利的例子。 我有一个包含 1 到 12 种产品的容器,每行 4
例如,它们在自动边距方面会导致完全不同的行为。 看看这个 fiddle :https://jsfiddle.net/L1rk46xy/ .fixed { display:fixed;
我尝试在帖子中将段落的宽度设置为 75%,并将图像的响应宽度设置为 100%。然而,总是在 默认。 Some texts Some texts Some texts Some texts 目前,我只
HTML 元素可以有 width/height 属性,也可以有 CSS width/height 属性: HTML 属性和 CSS 属性有什么区别,它们应该具有相同的效果吗? 最佳答案 有关该主题的
我有一个流动的 table ,现在需要一个固定的 thead。问题是当你固定 thead 时,th-s 不尊重 tbody 的 td-s 的宽度。列的大小都由 BootStrap 处理。我已经阅读了很
我想像这样布置一个区域: ---- ---- |A | |B | | | | | ---- ---- --------- |C | --------- 三个盒子中的每一个都是 .盒子
我遇到了很多问题。 1) 我正在使用 Bootstrap-Select 来获得具有搜索功能的现代选择框,但无论我尝试什么,我似乎都无法获得填充 col-span 的选择。 2) 我已将该行拆分为 2
http://jsfiddle.net/95EtZ/1/ 问题在行动中解决了一半。 现在它是用 javascript 中硬编码的容器宽度设置的。 我需要 js 来获取容器 div 的宽度——使用窗口滚
我想要两个宽度和高度均为 100% 的 div。我知道子 div 不会工作,因为父 div 没有特定的高度,但有没有办法解决这个问题? HTML: CSS: body
我需要使用 jQuery 更改 的高度和宽度 我尝试了以下代码 jQuery('#chart_popup').css('height','600px'); jQuery('#chart_popup')
在自定义 WPF 控件中,我想将控件的宽度设置为高度的函数。例如:Width = Height/3 * x; 实现此目的的最佳方法是什么,以便控件正确且流畅地调整大小(和初始大小)? 最佳答案 您可以
我正在使用igraph在R中绘制图形,执行plot(mygraph, vertex.color = "green")之类的操作。 有没有办法改变顶点边界的颜色和/或宽度? 最佳答案 查看下面的代码;
有没有办法使用jquery设置图像的高度和宽度?以下是我的代码 var img = new Image(); // Create image $(img).load(function(){
这个问题类似于 how-to-find-the-actual-width-of-grid-component-with-scrollbar-in-delphi 但我无法获取 CalcDrawInfo(
这里是 HTML/CSS 新手。 试图将我在 Codeacademy 上学到的知识付诸实践,但我遇到了一个问题,即我设置为 width:100% 的 header 最终离开了页面。我相信这是因为边框,
这里是 HTML/CSS 新手。 试图将我在 Codeacademy 上学到的知识付诸实践,但我遇到了一个问题,即我设置为 width:100% 的 header 最终离开了页面。我相信这是因为边框,
我是一名优秀的程序员,十分优秀!