标签: autolayout

如何使用自动布局约束使自定义宽度的按钮居中?

请参阅以下代码:

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

0
推荐指数
1
解决办法
6268
查看次数

使用Autolayout时改变UIButton的大小?

我对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'是我添加的)

uibutton xib uiview ios autolayout

0
推荐指数
1
解决办法
3739
查看次数

如何改变按钮的位置.Objective-C的

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)

我认为这是一个自动布局问题,但我不知道如何解决.

objective-c uibutton ios autolayout

0
推荐指数
1
解决办法
3565
查看次数

iOS应用程序无法在iOS 6或更低版本上运行

我正在手机上使用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)

iphone objective-c ios autolayout

0
推荐指数
1
解决办法
226
查看次数

使用动态UITextView,UIImageView和UILabel Heights的自动布局

我正在尝试创建一个包含一个带有2个标签和文本视图的图像视图的视图.每个元素的内容都将从SQLite数据库中读取,所以我想使用Xcode 5的自动布局来更改每个UI元素的高度以及它们之间的距离,根据每个元素的内容有效地使用屏幕空间尽可能.我有一些条件,我试图通过界面生成器中的自动布局强制执行,但我不知道如何实现它们:

  • 标签与其正下方的文本视图之间的距离将保持不变
  • 所有元素的宽度将保持不变,并且它们都将水平居中
  • 只有元素框架的高度和y值才会改变
  • 如果文本视图的内容高度超过了它们可以显示的屏幕高度,则应将图像视图的高度降低到最小值,然后降低文本视图的高度
  • 每个元素之间的距离应相等(不包括相关标签和文本视图之间的距离)

我可以通过编程方式完成所有这些操作,但我想使用自动布局,以便视图可以轻松适应屏幕大小的变化并防止错误.我对自动布局的经验很少,而且我遇到了在这种情况下我需要的复杂规格.为了使我想要创建的内容更清晰,这里是.xib文件的屏幕截图:

截图

谢谢您的帮助!

objective-c uitextview ios autolayout xcode5

0
推荐指数
1
解决办法
693
查看次数

IOS - 使用autolayout和drawRect

我正在使用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)

在动画块中,圆形像素看起来更大更难看.

我这样做的方法是否正确?

正确 不正确

core-graphics drawrect uiviewanimation ios autolayout

0
推荐指数
1
解决办法
2976
查看次数

iOS AutoLayout约束

我是autolayout的新手.有人可以解释为什么这不起作用?MAFeedbackCell是两个标签的容器.有在容器等标签,但是这两个:0xaa3aca00x14395240是有冲突的.

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)

ios autolayout

0
推荐指数
1
解决办法
119
查看次数

子视图垂直对齐视图中的自动布局约束

我需要在视图中找到子视图垂直对齐中间,而不使用编程方式使用约束硬编码顶部值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)

subview uiview ios autolayout

0
推荐指数
1
解决办法
7067
查看次数

traitCollection.userInterfaceIdiom返回-1

我想检查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)

我不知道问题出在哪里,我一直在搜索谷歌,但还没有发现这里发生了什么.

xcode ios autolayout ios8

0
推荐指数
1
解决办法
755
查看次数

如何通过故事板自动布局将tableview放到导航栏下面的superview

我想创建一个滚动禁用的tableview,它将填充导航栏下面的视图.最后我设法做到了,但这不是正确的方法,因为我给tableview提供了减去64的边距(状态栏+导航栏).

我正在从故事板中分配我的约束.我已经尝试了许多其他约束与表视图,如从4个边给出零约束或等宽+等高+中心水平+中心垂直没有一个工作.

解决这个问题的正确方法是什么.

故事板的屏幕截图如下.

在此输入图像描述 在此输入图像描述 在此输入图像描述

uitableview ios autolayout nslayoutconstraint xcode6

0
推荐指数
1
解决办法
4519
查看次数