我有一个 UITableViewController,我将 UIView 放在导航项的正下方和实际表格的上方。我遇到的问题是视图与表视图一起滚动。
我如何让它的行为与导航栏完全相同,并使表格视图中的项目在其后面滚动。
与其让视图滚动,不如让它保持在原来的位置,让所有东西都在它后面。抱歉重申,但我发现有时这是必要的。
我需要根据按下的按钮来更改 UI 元素,基本上它就像一个选项卡,但只是我的视图控制器的一部分。我需要显示/隐藏的元素是 UIView,其中包含多个集合视图和标签。
我现在正在做的是以 IB 和编程方式定义两个对象,当我需要显示一个对象时,我只需将其中一个对象的宽度设置为 0,并为另一个对象设置一个具有我想要的整个宽度的新框架。这种方法的问题在于,即使 UIView 是我想要的样子,里面的内容似乎仍保留先前的约束(居中),并且它们不会“适应”整个宽度。
当前代码:
var newFrame = self.view.frame
newFrame.origin.x = 0
newFrame.origin.y = self.blueView.frame.origin.y
newFrame.size.height = self.blueView.frame.size.height
if (showGreen){
self.greenView.frame = newFrame
self.greenView.hidden = false
self.blueView.frame.size.width = 0.0
self.blueView.hidden = true
}else{
self.blueView.frame = newFrame
self.blueView.hidden = false
self.greenView.frame.size.width = 0.0
self.greenView.hidden = true
}
Run Code Online (Sandbox Code Playgroud)
在 IB 中,我为两个视图设置了约束,使其与边距之间没有分隔。
我希望您帮助我解决此问题或以其他方式实现所需的行为。
谢谢。
我正在使用Storyboard与autolayout. 在我的中,ViewController我有一个基本结构:一个UIScrollView和一个包含不同标签的 contentView ( UIView)。然后我在viewDidAppearmy 类的方法中添加了不同的元素contentView,然后为了重新计算它的帧大小,我这样做:
- (void)fixContentViewHeight
{
_contentView.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y, _contentView.frame.size.width, operators.frame.origin.y + operators.frame.size.height);
//constraint for contentView height
_contentViewHeight.constant = operators.frame.origin.y + operators.frame.size.height;
}
- (void)fixScrollViewHeight
{
_scrollView.frame = _contentView.frame;
_scrollView.contentSize = _contentView.frame.size;
}
Run Code Online (Sandbox Code Playgroud)
其中operators是 最后放置的元素contentView,它始终位于框架的底部。我在 中调用这两个方法,viewDidAppear它们使视图可滚动,问题是 的框架contentView没有更新,因此最后一个元素始终不可单击(因为它们放置在视图框架之外)。
我究竟做错了什么?为什么scrollView变得可滚动但contentView保持旧框架?
private func setTitle(title:String, subtitle:String, animate: Bool) -> UIView {
let titleLabel = UILabel(frame: CGRect(x:0, y:-5, width:0, height:0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.gray
titleLabel.font = UIFont.boldSystemFont(ofSize: 15)
titleLabel.text = title
titleLabel.adjustsFontSizeToFitWidth = true
let subtitleLabel = UILabel(frame: CGRect(x:0, y:18, width:0, height:0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.black
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.adjustsFontSizeToFitWidth = true
let titleView = UIView(frame: CGRect(x:0, y:0, width:max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), height:30))
let titleViewTapGesture = UITapGestureRecognizer(target: self, action: #selector(titleViewTapped(sender:)))
titleView.addGestureRecognizer(titleViewTapGesture)
titleView.addSubview(titleLabel)
if animate{
subtitleLabel.alpha = 0.0 …Run Code Online (Sandbox Code Playgroud) 我已经在 IOS 应用程序的 UIView 中添加了 UIButton。它与目标操作配合得很好,但是当我按下按钮时,它没有在屏幕上显示按下的感觉。(当用户触摸普通按钮时,其标题将变暗)。
我使用了以下代码:
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setupViewLayouts()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupViewLayouts()
}
func setupViewLayouts() {
titleLabel?.font = UIFont.boldSystemFont(ofSize: 16.0)
backgroundColor = .black
layer.borderWidth = 1.0
layer.cornerRadius = 3.0
layer.borderColor = UIColor.clear.cgColor
setTitleColor(UIColor.black, for: .normal)
}
}
var btnLogin: CustomButton = {
let button = CustomButton()
button.setTitle("Login", for: .normal)
button.addTarget(self, action: #selector(btnLogin_Tapped), for: .touchUpInside)
return button
}()
addSubview(btnSignIn)
Run Code Online (Sandbox Code Playgroud) 我正在使用聊天应用程序的常规设计和 UI 构建一个聊天应用程序。
我的应用程序需要一个“工具栏”(0,0 375x66),我希望它在显示键盘时保持在原来的位置。它有 4 个约束:顶部:0、前导:0、尾随:0、纵横比:125:22
我有一个UICollectionView(0,66 375x530),我希望在显示键盘时像任何其他聊天应用程序一样滚动它。它有 4 个约束:顶部(到工具栏):0,前导:0,尾随:0,底部(到 newMessageView 我将很快解释):0
我有一个里面UIView有一个。UITextField我们称之为newMessageView(0,596 375x71),它有 4 个约束:底部:0、前导:0、尾随:0、纵横比:375:71
现在,我尝试newMessageView在显示键盘时打开应用程序。我正在尝试这样做:
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.newMessageViewBottomConstraint.constant += keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.newMessageViewBottomConstraint.constant -= keyboardSize.height
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它完全不起作用。视图跳跃和隐藏,我真的不明白为什么。
我做对了吗?有人可以帮助我并指导我吗?
我已经看到了类似问题的其他一些答案,但即使进行了一些调整,这些解决方案都对我不起作用。
正如标题所示,我希望我的UICollectionViewCells 有圆角和阴影(或除了顶部之外的所有侧面都有阴影)。到目前为止,我所做的是将两个视图添加到我的UICollectionViewCell-中mainView,它显示单元格的必要内容并具有圆角,并且shadowView,它具有阴影。两个视图都不是另一个视图的子视图,它们一起存在于同一个单元格中。它们的尺寸完全相同,并且mainView明显显示在 的顶部shadowView。这是我当前的代码实现:
let mainView = cell.viewWithTag(1003) as! UIView
mainView.layer.cornerRadius = 10.0
mainView.layer.borderWidth = 1.0
mainView.layer.borderColor = UIColor.clear.cgColor
mainView.layer.masksToBounds = true
let shadowView = cell.viewWithTag(1002) as! UIView
shadowView.layer.shadowColor = UIColor.black.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 2.0)
shadowView.layer.shadowRadius = 2.0
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = UIBezierPath(roundedRect: shadowView.bounds, cornerRadius: mainView.layer.cornerRadius).cgPath
Run Code Online (Sandbox Code Playgroud)
以下是这段代码产生的结果:
有人会认为mainView它的角不够圆,阴影不够大。
然而,在删除 的背景shadowView并将其设置UICollectionView为黑色后,您可以看到 的mainView角实际上非常圆滑:

所以这意味着问题在于 的shadowView …
如果我想创建一个具有自定义形状的自定义 UIView,我应该重写绘制函数,对吗?
但我不明白它是如何工作的,它没有返回值,我应该将贝塞尔路径发送到哪里?
override func draw(_ rect: CGRect) {
// after I create my bezier path what should I do inside to have my custom view drawn?
}
Run Code Online (Sandbox Code Playgroud)
预先感谢您的答复
我正在尝试创建一个UIView具有非矩形触摸区域的非矩形形状。我已经知道如何用贝塞尔路径绘制形状。
根据此评论,我需要重写point(inside:with:)以创建自定义形状的触摸区域。
所以我尝试了这个:
class MyView: UIView {
override func draw(_ rect: CGRect) {
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: 0, y: bounds.maxY))
path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.midY))
path.addLine(to: CGPoint(x: bounds.midX, y: bounds.midY))
path.addLine(to: CGPoint(x: bounds.midX, y: 0))
path.addLine(to: CGPoint(x: 0, y: 0))
UIColor.red.setFill()
path.fill()
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
print("point(inside:with:) called")
func isInRegion(_ point: CGPoint) -> Bool {
return (0...bounds.midX).contains(point.x) …Run Code Online (Sandbox Code Playgroud) 我正在开发应用程序,在其中显示 UITableView。UITableView 顶部有一个按钮过滤器。现在,单击该按钮时,我想要对 UIVIEW 进行动画处理,使其从屏幕右侧到屏幕左侧进行动画处理。并且它必须一直保持动画状态,直到达到屏幕宽度的 70%。
现在这样做会使上一个视图变得更暗,我想让外观和感觉与 Android 中的导航抽屉相同。但这只会显示该 UIView 中的过滤器设置。
我搜索了一下,有太多的库无法执行我想要的任务,但我想自己编写代码并为 UIView 或 Nib 设计设置动画来执行此操作。
无论如何,有没有办法在不使用任何库或任何东西的情况下执行我想要的操作?请让我知道我正在等待。提前致谢。
uiview ×10
ios ×9
swift ×8
cocoa-touch ×1
constraints ×1
draw ×1
iphone ×1
objective-c ×1
swift4 ×1
uibutton ×1
uilabel ×1
uitableview ×1