请参阅以下代码:
def viewDidLoad
super
self.view.translatesAutoresizingMaskIntoConstraints = false
self.view.backgroundColor = UIColor.whiteColor
@start = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap do |el|
el.translatesAutoresizingMaskIntoConstraints = false
el.setTitle('Start', forState:UIControlStateNormal)
el.addTarget(self, action:'toStartController', forControlEvents:UIControlEventTouchUpInside)
self.view.addSubview(el)
end
self.layout_subviews
end
def layout_subviews
metrics = { 'space' => 8 }
views_dict = {
'superview' => self.view,
'start' => @start
}
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat('H:|-[start(100)]-|',
options: NSLayoutFormatAlignAllCenterX,
metrics: metrics,
views: views_dict))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat('V:[start]-|',
options: NSLayoutFormatAlignAllBottom,
metrics: metrics,
views: views_dict))
end
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是H:|-[start(100)]-|行不通的.我想要的是一个宽度为100的按钮,以X轴为中心,并以默认边距连接到屏幕底部.一旦我删除(100)这个工作,但按钮伸展到屏幕的宽度减去默认边距.当我指定自定义宽度时,我认为自动布局系统无法确定左边距和右边距应该是什么.我收到一个Unable to simultaneously satisfy constraints.错误.我想这与破折号有关H:|-[start(100)]-|,它需要有一个流体宽度来将start元素附加到superview,而不是默认边距.
有关如何解决这个问题的任何想法?
更新(感谢ghettopia): …
autolayout rubymotion ios6 nslayoutconstraint visual-format-language
我对ios很新.我正在开发一个项目,它经常在整个应用程序中使用某个UIView类.这个类只是一个用背景颜色做一些透明度的图像.这到目前为止工作正常.这个类有时作为单独的UIView存在,有时作为按钮子视图存在.
当我将这个UIVut添加到UIButton作为子视图时,UIView的全部内容以完整大小显示,但按钮的可点击区域保持xib中定义的大小,除非关闭Use Autolayout(即使我手动尝试)设置按钮的框架.)
我想在xib上放置一个UIButton作为占位符,然后根据初始化的UIView的叠加图像的大小定义其大小/可点击区域.
这应该是可能的,还是我误解了xib文件/ autolayout的用法?
在ViewDidLoad下我有......
- (void)viewDidLoad{
[theButton addSubview:_theView];
CGRect buttonFrame = theButton.frame;
buttonFrame.size = CGSizeMake(_theView.getSize.width,_theView.getSize.height);
[theButton setFrame:buttonFrame];
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试调用setFrame之前和之后打印按钮信息时,框架保持相同的大小.
(注意'_theView.getSize'是我添加的)
stackoverflow上定位按钮的方法不起作用.我正在使用
self.todayButton.frame = CGRectMake(0, self.todayButton.frame.origin.y, self.todayButton.frame.size.width, self.todayButton.frame.size.height);
Run Code Online (Sandbox Code Playgroud)
我认为这是一个自动布局问题,但我不知道如何解决.
我正在手机上使用Xcode 5和iOS 7制作iPhone应用程序; 该应用程序适用于iOS 7.但是,当我在iPhone 6.1模拟器上运行该应用程序时,我收到此消息:
2013-09-01 10:46:30.075 Bars[30745:c07] 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:0x81b80a0 …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个包含一个带有2个标签和文本视图的图像视图的视图.每个元素的内容都将从SQLite数据库中读取,所以我想使用Xcode 5的自动布局来更改每个UI元素的高度以及它们之间的距离,根据每个元素的内容有效地使用屏幕空间尽可能.我有一些条件,我试图通过界面生成器中的自动布局强制执行,但我不知道如何实现它们:
我可以通过编程方式完成所有这些操作,但我想使用自动布局,以便视图可以轻松适应屏幕大小的变化并防止错误.我对自动布局的经验很少,而且我遇到了在这种情况下我需要的复杂规格.为了使我想要创建的内容更清晰,这里是.xib文件的屏幕截图:

谢谢您的帮助!
我正在使用autolayout,我有一个RoundView类(UIButton的子视图),其drawRect方法是:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat lineWidth = 0.0;
CGContextSetLineWidth(context, lineWidth);
CGContextSetFillColorWithColor(context, _currentBackgroundColor.CGColor);
CGContextAddEllipseInRect(context, self.bounds);
CGContextFillPath(context);
}
Run Code Online (Sandbox Code Playgroud)
然后我将它添加到self(一个UIView),使用autolayout设置其宽度和高度.一切看起来都很棒.但是当我改变它的大小限制(有更大的视图),并调用
[self layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)
在动画块中,圆形像素看起来更大更难看.
我这样做的方法是否正确?

我是autolayout的新手.有人可以解释为什么这不起作用?MAFeedbackCell是两个标签的容器.有在容器等标签,但是这两个:0xaa3aca0和0x14395240是有冲突的.
2014-09-28 08:38:26.056 beats[1052:60b] 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)
(
"<NSLayoutConstraint:0xaa3aea0 UILabel:0xaa3aca0.width …Run Code Online (Sandbox Code Playgroud) 我需要在视图中找到子视图垂直对齐中间,而不使用编程方式使用约束硬编码顶部值150.我希望实现如下所示:

以下是我的代码到目前为止,请指教.
import UIKit
class MainViewController: UIViewController {
var viewInner:UIView = UIView()
var viewOuter:UIView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
viewInner.backgroundColor = UIColor.redColor()
viewOuter.backgroundColor = UIColor.purpleColor()
viewOuter.frame = CGRectMake(100, 100, 400, 400)
viewInner.setTranslatesAutoresizingMaskIntoConstraints(false)
let viewsDictionary = ["viewInner":viewInner]
viewOuter.addSubview(viewInner)
self.view.addSubview(viewOuter)
//Add Constraint
var constOuterH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[viewInner(>=200)]-10-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
var constOuterV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-150-[viewInner(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
viewOuter.addConstraints(constOuterH)
viewOuter.addConstraints(constOuterV)
}
}
Run Code Online (Sandbox Code Playgroud) 我想检查iPhone或iPad是否使用这些代码
if traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
println("iPhone")
} else {
println("iPad")
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用iPhone模拟器和我的iPhone,但它打印了"iPad"
然后我试着打印"traitCollection.userInterfaceIdiom"
println(traitCollection.userInterfaceIdiom.rawValue)
Run Code Online (Sandbox Code Playgroud)
它打印"-1"而不是"0"(iPhone)或"1"(iPad)
我不知道问题出在哪里,我一直在搜索谷歌,但还没有发现这里发生了什么.
我想创建一个滚动禁用的tableview,它将填充导航栏下面的视图.最后我设法做到了,但这不是正确的方法,因为我给tableview提供了减去64的边距(状态栏+导航栏).
我正在从故事板中分配我的约束.我已经尝试了许多其他约束与表视图,如从4个边给出零约束或等宽+等高+中心水平+中心垂直没有一个工作.
解决这个问题的正确方法是什么.
故事板的屏幕截图如下.

autolayout ×10
ios ×9
objective-c ×3
uibutton ×2
uiview ×2
drawrect ×1
ios6 ×1
ios8 ×1
iphone ×1
rubymotion ×1
subview ×1
uitableview ×1
uitextview ×1
xcode ×1
xcode5 ×1
xcode6 ×1
xib ×1