标签: nsautolayout

子视图可以使用自动布局自动调整其超级视图的大小吗?

如果您有一个没有约束的视图,并且该视图具有相同大小的左,右,上,下约束的子视图,如果子视图的大小增加,则超视图的大小可以自动增加以适应这些约束?

谢谢.

ios autolayout nsautolayout

7
推荐指数
1
解决办法
5993
查看次数

可视化格式语言,按钮宽度和高度约束

有没有办法用NSLayoutConstraints设置UIView的宽度和高度?例如,我有以下约束设置

subView.setTranslatesAutoresizingMaskIntoConstraints(false);
        let cons:NSArray =   NSLayoutConstraint.constraintsWithVisualFormat("[subView(99)]", options:nil, metrics: nil, views: ["subView":subView]);
        self.view.addConstraints(cons);
Run Code Online (Sandbox Code Playgroud)

这只设置宽度,如何添加高度?

objective-c ios nslayoutconstraint nsautolayout swift

7
推荐指数
2
解决办法
7625
查看次数

将标签设置为UITableView的tableFooterView - 使用autolayout

当我的表视图为空时,我想在tableview页脚中添加一个简单的标签,以显示"无内容"消息.当我设置UILabel的框架和我用于footerView的UIView时它工作正常,但现在我正在尝试转换为使用自动布局,我无法让它工作.

这是我正在做的事情:

// Create the footer    
UIView *tempFooter = [[UIView alloc] init];
UILabel *atext = [[UILabel alloc] init];
tempFooter.translatesAutoresizingMaskIntoConstraints = NO;
atext.translatesAutoresizingMaskIntoConstraints = NO;

atext.numberOfLines = 0;
[atext setText:@"Add Some Text"];
atext.textAlignment = NSTextAlignmentCenter;
atext.font = [UIFont italicSystemFontOfSize:14];
atext.textColor = [UIColor grayColor];
atext.backgroundColor = [UIColor clearColor];
atext.lineBreakMode = NSLineBreakByWordWrapping;

// add label to footer view, and add constraints
[tempFooter addSubview:atext];
NSLayoutConstraint *tvatt1 = [NSLayoutConstraint constraintWithItem:tempFooter
                                                          attribute:NSLayoutAttributeLeft
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:atext attribute:NSLayoutAttributeLeft
                                                         multiplier:1.0
                                                           constant:10.0];

NSLayoutConstraint *tvatt2 = [NSLayoutConstraint constraintWithItem:tempFooter
                                                          attribute:NSLayoutAttributeRight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:atext
                                                          attribute:NSLayoutAttributeRight …
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview uikit nslayoutconstraint nsautolayout

6
推荐指数
1
解决办法
1万
查看次数

NSLayoutAttribute的解释

我发现文档有点稀疏.这是一个例子

NSLayoutAttributeTrailing
  The trailing edge of the object’s alignment rectangle.
  Available in iOS 6.0 and later.
  Declared in NSLayoutConstraint.h
Run Code Online (Sandbox Code Playgroud)

具体来说,我希望所有这些解释

NSLayoutAttributeLeft
NSLayoutAttributeRight
NSLayoutAttributeTop
NSLayoutAttributeBottom
NSLayoutAttributeLeading
NSLayoutAttributeTrailing
NSLayoutAttributeWidth
NSLayoutAttributeHeight
NSLayoutAttributeCenterX
NSLayoutAttributeCenterY
NSLayoutAttributeBaseline
NSLayoutAttributeLastBaseline
NSLayoutAttributeFirstBaseline
NSLayoutAttributeLeftMargin
NSLayoutAttributeRightMargin 
NSLayoutAttributeTopMargin
NSLayoutAttributeBottomMargin 
NSLayoutAttributeLeadingMargin 
NSLayoutAttributeTrailingMargin 
NSLayoutAttributeCenterXWithinMargins
NSLayoutAttributeCenterYWithinMargins 
Run Code Online (Sandbox Code Playgroud)

ios nsautolayout

6
推荐指数
1
解决办法
2831
查看次数

NSAutoLayoutConstraints崩溃的应用程序

我已经按照了一些教程,但我似乎无法让我的应用程序接受我为NSAutoLayoutConstraints设定的规则.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
V:|-[signupBtn][signupWithFacebookBtn]-| 
Run Code Online (Sandbox Code Playgroud)

在我的viewLoad方法中:

-(void)loadView
{
    [super loadView];



    //Create the background view
    [self.view setBackgroundColor:[UIColor whiteColor]];


    //Set up the buttons, labels, and textfields
    signupBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [signupBtn setTitle:@"Sign Up!" forState:UIControlStateNormal];
    [signupBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [signupBtn setFrame:CGRectMake(50, 350, 220, 40)];
    signupWithFacebookBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [signupWithFacebookBtn setTitle:@"Sign Up with Facebook" forState:UIControlStateNormal];
    [signupWithFacebookBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [signupWithFacebookBtn setFrame:CGRectMake(50, 400, 320, 40)]; …
Run Code Online (Sandbox Code Playgroud)

objective-c ios nsautolayout

6
推荐指数
1
解决办法
5261
查看次数

自动布局:某些视图为空时自动更新约束

我有这样的看法

 _______________________________
|     |                         |
|  Label 0                      |
|     ^                         |
|  Label 1                      |
|     ^                         |
|  Label 2                      |
|     ^                         |
|  Label N                      |
|_______________________________|
Run Code Online (Sandbox Code Playgroud)

^代表将的顶部Label X+1与的底部链接的约束Label XLabel 0用固定常数约束到超级视图的顶部。

我要实现的目标是:如果某些内容Label X为空,则Label X+1必须取代它。

 _______________________________
|     |                         |
|  Label 0 (empty)              |
|     ^                         |
|  Label 1                      |
|     ^                         |
|  Label 2                      |
|     ^                         |
|  Label N                      |
|_______________________________| …
Run Code Online (Sandbox Code Playgroud)

ios autolayout nsautolayout

5
推荐指数
1
解决办法
906
查看次数

iOS自动布局 - 将位于tableviewcell内的视图移动到屏幕中心

我有一个包含文本视图和图像视图的单元格的tableview.我的项目目前正在使用AutoLayout.我的目标是在点击时将图像视图全屏显示.一个选项是使用模态视图控制器,但我想让这项工作有点像在Facebook应用程序中点击图像的方式工作,应用程序使图像居中并淡化背景.

由于我使用autolayout,我不能简单地设置imageview的框架来填充屏幕.相反,我需要使用自动布局约束.我的图像视图有5个约束,约束设置距单元格底部的距离,以及左侧和右侧,以及控制图像高度的约束.最后一个是图像视图上方的文本视图和图像顶部之间的垂直空间约束.虽然这似乎与高度和底部约束相冲突,但由于某种原因,界面构建器迫使我有这个.为了避免出现问题,我将此约束的优先级设置为小于1000(无论如何,图像永远不会与textview重叠,因为tableview单元格高度设置为所有内容都完全适合).

为了使图像居中,我将左右距离设置为零,并删除垂直空间约束.为了使图像居中,我将带有中心y对齐约束的底部空间约束替换为UIWindow而不是tableviewcell.我希望它在屏幕的中心,而不是细胞.

要获取主窗口,我使用此:

AppDelegate* myDelegate = (((AppDelegate*) [UIApplication sharedApplication].delegate));
//access main window using myDelegate.window
Run Code Online (Sandbox Code Playgroud)

然后,设置约束:

//currently sets the distance from the bottom of the cell to 14
//changing it...
[cellselected removeConstraint:cellselected.imagebottomspace];
cellselected.imagebottomspace = [NSLayoutConstraint constraintWithItem:cellselected.viewimage attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:myDelegate.window attribute:NSLayoutAttributeCenterY multiplier:0 constant:0];
[cellselected addConstraint:cellselected.imagebottomspace];
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用.图像视图的宽度和高度的变化适用.然而,当读取imagebottomspace约束时,我得到一个不可满足的布局 - 显然约束与另一个约束冲突,该约束将底部和图像视图之间的距离设置为14,这是我刚刚删除的约束.所以它似乎并没有实际删除约束.

当我继续让app破坏约束时,imageview会移动,但会移动到错误的位置.它不在屏幕中心.它向上移动并离开屏幕.

显然我正在做的事情是不对的.我究竟做错了什么?

uitableview ios autolayout nsautolayout

4
推荐指数
1
解决办法
1465
查看次数

当UIView获得框架时,Autolayout会检测到

我有UIView自动布局约束.我用框架创建它CGRectZero.所以我不是真正的大小.这将取决于superview.

但我想知道我的视图何时获得一个可用于计算的框架.

ios autolayout nsautolayout ios7 ios-autolayout

4
推荐指数
1
解决办法
5557
查看次数

使用AutoLayout以编程方式将多行UILabel的第一行与UIImage对齐(Objective-C)

我正在尝试将多行UILabel与UIImage对齐.问题是:我希望水平地将此标签的第一行与图像对齐.

我尝试了什么:

1 - 将图像与整个UILabel对齐,所以基本上我仍然想要拍摄图像.

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[image(width)]-8-[multiLinesLabel]|" options:NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];
Run Code Online (Sandbox Code Playgroud)

2 - 所以我尝试了这个,但后来图像太高了.

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[image(width)]-8-[multiLinesLabel]|" options:NSLayoutFormatAlignAllTop metrics:metrics views:views]];
Run Code Online (Sandbox Code Playgroud)

有人有想法这样做吗?

提前致谢

编辑:我终于做到了这样的伎俩:

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[image(width)]-8-[multiLinesLabel]|" options:kNilOptions metrics:metrics views:views]];

[containerView addConstraint:[NSLayoutConstraint
                                      constraintWithItem:image
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:multiLinesLabel
                                      attribute:NSLayoutAttributeTop
                                      multiplier:1.0
                                      constant:3.0]];
Run Code Online (Sandbox Code Playgroud)

我并不为我感到骄傲,这不是一个解决问题的干净方法,但它确实有效.如果有人想到其他解决方案......

objective-c ios nsautolayout

4
推荐指数
1
解决办法
1877
查看次数

多行NSTextField不起作用

我一直在尝试使用多行NSTextField自动布局preferredMaxLayoutWidth。我不知道为什么这不起作用。

class ViewController: NSViewController {
   override func viewDidLoad() {
      super.viewDidLoad()

      let textField = NSTextField()
      textField.cell!.usesSingleLineMode = false
      textField.cell!.wraps = true
      textField.cell!.lineBreakMode = .byCharWrapping
      view.addSubview(textField)

      textField.translatesAutoresizingMaskIntoConstraints = false
      NSLayoutConstraint.activate([
         textField.topAnchor.constraintEqual(to: view.topAnchor),
         textField.leadingAnchor.constraintEqual(to: view.leadingAnchor),
         textField.widthAnchor.constraintEqual(toConstant: 20)
      ])
      textField.preferredMaxLayoutWidth = 20
      textField.stringValue = "abcdefghijklmnopqrstuvwxyz"

      view.needsLayout = true
      view.layoutSubtreeIfNeeded()
      print("Intrinsic content size: \(textField.intrinsicContentSize)")
      print("Fitting size: \(textField.fittingSize)")
   }
}
Run Code Online (Sandbox Code Playgroud)

打印:

本质内容大小:(-1.0,21.0)

合身尺寸:(20.0,21.0)

(21.0是一行的大小。)

macos cocoa nstextfield autolayout nsautolayout

3
推荐指数
2
解决办法
3396
查看次数