我有一个图像视图,我在屏幕的中间右侧排列.使用约束添加图像视图时,一切都很好.我需要将图像视图从原始位置(点A)动画到屏幕的底部(点B); 问题是当我尝试从A点动画到B点时,图像视图从屏幕的左上角开始,就像它只是简单地添加没有约束(而不是我希望它在中央) .
这是代码和一些评论:
#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0) //defined above
- (void)viewDidLoad
{
UIImage *cardFaceDownImage = [UIImage imageNamed:@"b-top@2x.png"];
UIImageView *dealCardDown = [[UIImageView alloc] initWithImage:cardFaceDownImage];
dealCardDown.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:1];
NSLayoutConstraint *dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:130];
[self.view addConstraint:dealCardConstraintY];
[self.view addConstraint:dealCardConstraintX];
[self.view addSubview:dealCardDown];
//IF I comment out the below code the UIImageView lines up where I want it (Point A)
//IF I don't coment it …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用NSLayoutConstraints 设置视图的布局.
我可以在视图中正确设置,但是如果约束需要,我还需要能够调整当前视图(我正在设置约束的视图).换句话说,我想用灵活的窗口设置固定的内容大小,而不是反过来.
基本上,我有:
NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:self.v1, @"v1",
self.v2, @"v2",
nil];
// Set horizontal constraints
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[v1]-|"
options:0
metrics:nil
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[v2]-|"
options:0
metrics:nil
views:views]];
// Set vertical constraints
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[v1]-[v2]-|"
options:0
metrics:nil
views:views]];
// Initialise height constraint for v1
self.v1Height = [NSLayoutConstraint constraintWithItem:self.v1
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:50];
Run Code Online (Sandbox Code Playgroud)
如果我稍后更改self.v1Height(通过删除它,重新创建它,然后读取它),我希望框架self扩展(或收缩)以适应更改的高度.
我不确定它是否相关(因为我比iOS更熟悉iOS),但这是一个内容视图NSPopover.
为实现这一目标,我需要添加什么约束?
我试图通过自动布局约束来实现以下设计:

中间标签很简单(水平居中查看).
左右标签不是那么简单.简单地添加20的前导和尾随空间约束将不适用于所有内容.此外,下面的细节标签可以更宽.
我考虑给左右标签提供> = 20的前导和尾随空间约束并使文本居中.
如何使用动态内容实现3个标签的列布局:
我有一个带有tableView的ViewController.我已经在故事板中进行了设置.有没有办法以编程方式设置tableView的约束?我试图从ViewController中的tableView设置一个IBOutlet并为其添加约束.但那没用.
这是我的ViewController
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
addTableViewConstraints()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addTableViewConstraints() {
tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
var topConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)
var leadingContraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
var trailingConstraint = NSLayoutConstraint(item: self.tableView, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在故事板中创建复杂的用户界面.我得到的只是一堆错误,我不知道如何解决它们,因为建议的约束是不合适的.这是我项目的链接:链接.最终布局应该与图像中的布局类似:
简短版本:布局应该看起来像任何屏幕尺寸的图片中的布局.在较大的屏幕上,图像视图和彩色视图都应该变大.所有图像视图的大小应相同.
长版:所有图像视图的大小应相同.
大白视图应始终占据屏幕的上半部分.我知道该怎么做.红色视图应该恰好出现在超级视图的中心,即大白视图.
两个蓝色视图的宽度应与红色视图的宽度完全相同.这3个视图的宽度应与2个绿色视图的高度相匹配.意思是,屏幕越高(屏幕越高意味着绿色视图越高),ImageViews之间的空间越大(其中包含Image字的人,基本上意味着更宽的蓝色和红色视图).
左侧和右侧的白色视图将左侧图像视图的空间填充到左侧边距,右侧图像视图填充到右侧边缘.
所有视图的第一个邻居都为0.蓝色和红色是水平的,绿色的视图是垂直的.此外,底部和上部图像视图分别被打到superview(大白视图)的底部和顶部.
我不想在图像视图上设置高度和宽度约束,因为它们应该在运行时被解除限制.
如果有人能帮助我,我真的很感激!
ios uistoryboard autolayout nslayoutconstraint universal-storyboard
这是一个UICollectionView,紫色的单元格:
很简单,我希望单元格是集合视图宽度的1/2.(所以TBC,它将是集合视图中两行排列的单元格.)
(集合视图只是全屏,因此每个单元格都是屏幕宽度的一半.)
你如何在故事板中做到这一点?
如果我尝试以正常方式控制拖动,它基本上不起作用.
这些是简单的完全静态单元(非动态).
对于任何在Google上搜索的人来说,为了节省您的时间:这是(2016)最简单的方法来制作两个跨越UICollectionView的布局; 细胞之间没有间隙.
// Two - two-across UICollectionView
// use a completely standard UIViewController on the storyboard,
// likely change scroll direction to vertical.
// name the cell identifier "cellTwo" on the storyboard
import UIKit
class Two:UICollectionViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let w = collectionView!.bounds.width / 2.0
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width:w,height:w)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = …Run Code Online (Sandbox Code Playgroud) cocoa-touch ios uistoryboard uicollectionview nslayoutconstraint
我想添加一个按钮作为根视图的子视图.按钮必须水平放置在中心以及垂直于superview的中心.我以编程方式使用NSLayoutConstraints.这是我的代码.
import UIKit
class ViewController: UIViewController {
var button : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
button = UIButton()
button.setTitle("Hello World", forState: .Normal)
button.backgroundColor = UIColor.blueColor()
button.sizeToFit()
self.view.addSubview(button)
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0), …Run Code Online (Sandbox Code Playgroud) 我有一个UITableViewCell,里面有一个嵌入式UIStackView.一切正常,手机和iPad都很好看.但是,我在StoryBoard中遇到了很多奇怪的错误,我不知道是否可以忽略它们.通常,我讨厌忽视警告.但在这种情况下可以吗?我的表格单元格设置如下:
UITableViewCell
- Content View
-- View (named outerView)
--- View (named dateView for top banner color)
--- UIStackView (vertical, named mainStackView, pinned to neighbors)
---- UIButton (A)
---- UIView (B)
---- UIStackView (C, horizontal)
---- UIStackView (D, horizontal)
---- UILabel (E)
---- UIButton (F)
Run Code Online (Sandbox Code Playgroud)
mainStackView(字母AF)下的所有内容都在StoryBoard中显示错误,他们"需要约束:Y位置或高度".但是,根据Apple 文档, 它表示如果UIStackView是垂直方向,它们将处理所有约束和垂直对齐.就像我说的,它工作正常,arrangeSubviews可以折叠/隐藏,没有其他警告.我只是不喜欢发布带有警告的代码.我该怎么做才能解决这个问题,或者这是一个已知的问题?
更新:以下是评论所要求的MCVE.单击此处并解压缩该文件夹以获取仅在Interface Builder中可用的裸骨骼项目.
如果我有这个用于子视图控制器:
autoCompleteViewController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
autoCompleteViewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
autoCompleteViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
autoCompleteViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
autoCompleteViewController.view.bottomAnchor.constraint(equalTo: googleMapViewController.view.topAnchor, constant: 0),
autoCompleteViewController.view.heightAnchor.constraint(equalToConstant: 44.0)
])
Run Code Online (Sandbox Code Playgroud)
如何更改其高度并更新heightAnchor?我已经试过了:
autoCompleteViewController
.view
.heightAnchor
.constraint(equalToConstant: initialHeightAutoCompleteViewController + CGFloat(numberOfSuggestions * 45))
.isActive = true
Run Code Online (Sandbox Code Playgroud)
但没有运气。我也尝试添加layoutIfNeeded()和其他一些类似的方法,但是没有用。如何使用锚点更新视图高度?感谢您的回答。
我有一个自定义的UIView,我在我的ViewController上加载.
func loadBottomSheet(){
bottomSheet = Bundle.main.loadNibNamed("BottomSheetTest", owner: self, options: nil)![0] as! BottomSheetTest
bottomSheet?.setUp(parentController: self)
bottomSheet?.tag = 9009164
self.view.addSubview(bottomSheet!)
}
Run Code Online (Sandbox Code Playgroud)
自定义视图的代码如下:
func setUp(parentController: UIViewController){
self.parentController = parentController;
self.translatesAutoresizingMaskIntoConstraints = false
self.leftAnchor.constraint(equalTo: parentController.view.leftAnchor).isActive = true
self.rightAnchor.constraint(equalTo: parentController.view.rightAnchor).isActive = true
self.heightAnchor.constraint(equalToConstant: CGFloat(400)).isActive = true
self.bottomAnchor.constraint(equalTo: parentController.view.bottomAnchor).isActive = true
}
Run Code Online (Sandbox Code Playgroud)
我试图将它添加到我的视图控制器.以下代码使用一般错误使应用程序崩溃:libc ++ abi.dylib:以NSException类型的未捕获异常终止.
此时我已从自定义视图中删除了所有内容.你在这里看到的就是它的全部内容,没有更多的UI元素,没有IBOutlets,就是这样.我在机智的尽头.
ios ×9
autolayout ×4
uistoryboard ×3
swift ×2
cocoa ×1
cocoa-touch ×1
ios8 ×1
objective-c ×1
uiimageview ×1
uilabel ×1
uistackview ×1
uitableview ×1
uiview ×1