如何打印出锚点常量的值?例如:
let myButton = UIButton()
myButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 5).isActive = true
myButton.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
myButton.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
myButton.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
print(myButton.topAnchor.constant)//this would print 5
Run Code Online (Sandbox Code Playgroud) 我的应用程序应支持使用iOS 5和iOS 6的iPhone分辨率(5及更早版本).当我在IB中使用自动布局时,iOS 5应用程序崩溃如下:链接
所以,我试图在代码中使用NSLayoutConstraint,但还没有成功.
子问题:
谁知道怎么做?
PS:我知道可以通过设置框架手动制作,但我想使用NSLayoutConstraints.
UDP:是的,我知道,这就是我在代码中使用NSLayoutConstraint的原因.我可以为确定iOS版本做出条件.问题是关于iOS6.
我制作了针对iOS 6的iPhone应用程序
我的应用程序运行非常顺利,并且在应用程序中以及应用程序中的任何位置都没有崩溃..但我的应用程序显示了一些与之相关的警告autolayout.
我试了很多来解决这个警告,但我没有成功..
NSLog中的警告:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0xac85e50 h=-&- v=-&- …Run Code Online (Sandbox Code Playgroud) 我有一个容器视图:
我有一个表格视图,我以编程方式添加到容器中:
self.searchResultsSourcesTVC = [storyboard instantiateViewControllerWithIdentifier:@"searchResultsSourcesTVC"];
[self.searchResultsContainer addSubview:self.searchResultsSourcesTVC.view];
Run Code Online (Sandbox Code Playgroud)
这样的结果是表视图不会自动调整大小以适应容器; 它似乎在屏幕的南边延伸了很多,只要滚动条可以完全消失在屏幕的南边.但它确实显示了表格和搜索结果.
所以我试图添加一个约束(我使用自动布局)使表视图的垂直边界与容器视图的垂直边界匹配:
UITableView *tableView = self.searchResultsSourcesTVC.tableView;
NSDictionary *views = NSDictionaryOfVariableBindings(tableView);
tableView.translatesAutoresizingMaskIntoConstraints = NO; // without this line there are conflicts
[self.searchResultsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:Nil views:views]];
[self.view layoutIfNeeded]; // not sure whether this line is necessary
Run Code Online (Sandbox Code Playgroud)
现在根本没有桌子.只是一个空白的视图.
我究竟做错了什么?以编程方式将表视图添加到容器视图并使表视图的边界与容器视图的边界共同延伸的最佳方法是什么?谢谢
我对自动布局有疑问.我正在使用xib文件,我有一个像这样的视图控制器.

它嵌入在一个内部,UINavigationController所以我的按钮定位有Top Space to Superview约束.
问题是当我旋转设备时,它看起来像这样.

正如您所看到的那样,约束仍保持其原始值,因此导航栏边缘与横向模式下的按钮之间存在较大差距.
如何使按钮位置靠近导航栏,就像它在纵向中一样,并且在纵向和横向模式下都是这样的?我顺便使用Xcode 6.
谢谢.
我试图在视图中水平对齐两到三个按钮.为简单起见,我将展示我对齐两个按钮的尝试.
这适用于标题较短的按钮:
@"H:|-10-[questionButton1(questionButton2)]-5-[questionButton2]-10-|"
Run Code Online (Sandbox Code Playgroud)

但是只要其中一个按钮获得更长的标题,就会像这样打破:

我最终做的是计算每个按钮的宽度,然后如果button1宽度大于视图的一半并且大于button2宽度,我使用了:
@"H:|-10-[questionButton1(==btn1width)]-5-[questionButton2(>=btn2width)]-10-|"
Run Code Online (Sandbox Code Playgroud)
它有点工作,但我不喜欢这种计算的代码外观.想象一下它增加了第三个按钮的复杂性.此外,如果两个按钮都具有相当长的标题,则存在一个问题,在这种情况下,我必须弄清楚是否应该减小字体大小以使一切都适合.
我在这里发布这个是因为我可能会遗漏一些关于autolayout的神奇之处,因为我今天才开始在代码中使用它.任何形式的帮助将不胜感激.
---更新(澄清)---
考虑到边距(外侧10个,按钮5个),我希望按钮均匀分割.理想情况下,如果文本大小适合其默认大小,它们应该是相同的宽度(50%:两个按钮为50%,33%:33%:三个按钮为33%).如果按钮标题超过完美宽度,如果其他按钮允许按钮(如果其他按钮可以缩小),按钮应该扩展其宽度.如果没有可能的扩展或缩小,则大按钮应减小字体大小并重复该过程(检查其他按钮是否可以缩小).是的,我知道,我要求很多:)

---更新---
@Sikhapol的回答帮我解决了.我添加了一些东西来减少字体大小,添加填充并使按钮标题在文本不适合时分成多行:
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
btn.titleLabel.adjustsFontSizeToFitWidth = YES;
btn.titleLabel.numberOfLines = 0;
btn.titleLabel.minimumScaleFactor = 0.7;
Run Code Online (Sandbox Code Playgroud)
最终结果:

如何UIView使用自动布局和Apple的可视格式语言设置谁的大小和位置的宽度和高度?
这是代码(view只是变量UIViewController):
// Create and add the view
var stageView = UIView()
stageView.setTranslatesAutoresizingMaskIntoConstraints(false) // Since I'm using Auto Layout I turn this off
view.addSubview(stageView)
// Create and add constraints to the containing view
let viewsDictionary = ["stageView":stageView]
let horizontalConstraints: NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-150-[stageView]-150-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
let verticalConstraints: NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[stageView]-150-|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: viewsDictionary)
view.addConstraints(horizontalConstraints)
view.addConstraints(verticalConstraints)
println("stageView.frame=\(stageView.frame)")
Run Code Online (Sandbox Code Playgroud)
得到了:
stageView.frame=(0.0,0.0,0.0,0.0)
Run Code Online (Sandbox Code Playgroud)
所以我试过了:
let fittingSize = stageView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
println("fittingSize=\(fittingSize)")
Run Code Online (Sandbox Code Playgroud)
得到了:
fittingSize=(0.0,0.0)
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到一种方法来获得大小.我可以stageView …
ios autolayout nslayoutconstraint visual-format-language swift
我在故事板中偶然发现了一个有趣的问题.我正在使用约束.我想将视图高度设置为等于另一个视图的宽度.高度和宽度本身不是使用约束设置的,将根据屏幕大小在运行时设置.这可以在故事板中完成吗?
我遇到了自动布局问题,"无法同时满足约束".错误.
现在我已经为我的所有约束添加了标识符,但是,冲突的约束似乎不是由我创建的,因为我只在几个冲突的约束中看到了我的标识符.
我的问题是,如果我对约束有这样的描述:
<NSLayoutConstraint:0x7f8dca498410 V:[UIView:0x7f8dca493010]-(0)-[UILabel:0x7f8dca493610]>
我怎么能找出这是什么UIView以及这是哪个UILabel.我知道我可以做po 0x7f8dca493010,但是如何打印我的视图的属性,最好是打印例如我给IB中的视图的名称?
对于Swift中的iOS应用,我正在使用程序约束,并想将CAGradientLayer()添加到UIView()。下面是我的代码不起作用。
import UIKit
class ViewController: UIViewController {
let animationView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(animationView)
setupAnimationView()
animationView.addGradientLayer()
}
func setupAnimationView() {
animationView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
animationView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
animationView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
animationView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
extension UIView {
func addGradientLayer() {
let color1 = UIColor(red: 251/255, green: 55/255, blue: 91/255, alpha: 1.0)
let color2 = UIColor(red: 1.0, green: 70/255, blue: 0, alpha: 1.0) …Run Code Online (Sandbox Code Playgroud) ios ×8
autolayout ×7
swift ×3
ios6 ×2
objective-c ×2
constraints ×1
ipad ×1
iphone ×1
layout ×1
uistoryboard ×1
warnings ×1
xcode ×1