我有autolayout(也许)和我的scrollview的问题!
我的问题

然后我推到另一个视图
然后我回去看看它的滚动视图,我无法滚动到最高点.(我在滚动视图的弹跳中看到它)

有谁能够帮我?
我正在尝试使用新推出的iOS 6自动布局API构建一个相当简单的动画自定义UI.我正在构建的自定义视图有一个圆圈,我想要垂直和水平居中.
不幸的是我无法弄清楚为什么我的约束似乎对UIButton和UILabel元素工作正常,但是当我使用自定义视图和自定义CALayer(在这种情况下是一个圆圈,最终会被动画化)时会产生奇怪的结果.
要明确我不希望我的视图扩展以填充整个屏幕,而是要有动态的"填充",以便视图在iPhone 4和5上都垂直居中.我还应该注意到我非常Cocoa和UIKit的新手.
RootViewController.m:
...
- (void)viewDidLoad {
[super viewDidLoad];
// Create Circle View
CGRect circle_view_rect = CGRectMake(0, 0, 100, 100);
UIView *circle_view = [[UIView alloc] initWithFrame:circle_view_rect];
// Create Circle Layer
CircleLayer *circle_layer = [[CircleLayer alloc] init];
circle_layer.needsDisplayOnBoundsChange = YES;
circle_layer.frame = circle_view.bounds;
[circle_view.layer addSublayer:circle_layer];
// Enable Auto Layout
[circle_view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:circle_view];
// Center Vertically
NSLayoutConstraint *centerYConstraint =
[NSLayoutConstraint constraintWithItem:circle_view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0];
[self.view addConstraint:centerYConstraint];
// Center Horizontally
NSLayoutConstraint *centerXConstraint =
[NSLayoutConstraint constraintWithItem:circle_view
attribute:NSLayoutAttributeCenterX …Run Code Online (Sandbox Code Playgroud) 我在IB中有一个UIView,其中有另一个UIView,我用它作为容器视图.在代码中,我创建了三个不同的视图,然后根据应用程序的状态将相应的视图设置为容器视图中的动画.在任何给定时间,三种不同视图中只有一种是有效的.问题是,当我在模拟器中运行不同的iPhone时,我的新子视图不会缩放以匹配容器视图.我正在使用autolayout.出于测试目的,我将我的子视图设置为一个大按钮,它的所有边缘都限制在超视图中.并且容器视图的边缘也受限于它的超视图.我想要的是子视图匹配容器的视图.即按钮拉伸容器视图的整个大小.当在不同的iPhone上运行时,容器视图的大小以及子视图应该相对于不同的iPhone屏幕大小按比例缩放.
下面是我用来初始化子视图并设置相对于容器视图的约束的代码.
UIView *containerView = self.subView;
UIView *newSubview = [[mySubview alloc] init];
[newSubview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.containerView addSubview:newSubview];
[self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:newSubview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:newSubview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:newSubview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
[self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:newSubview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.containerView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
Run Code Online (Sandbox Code Playgroud)
我似乎无法让这个工作.我对autolayout很新,我不确定我做错了什么,并且想停止撞到这堵墙.任何帮助都会很棒.:)
*************附加信息**************
对不起,我没有尽可能清楚地说出我的问题.所以这里有更多关于屏幕截图的信息.首先,我将描述我在代码方面做了什么.
在AppDelegate.m中的didFinishLaunchingWithOptions中,我创建了这样的MyViewController,
self.myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
Run Code Online (Sandbox Code Playgroud)
在MyViewController.m中的viewDidLoad中,我创建了mySubview并将其添加到我的containerView并为此创建约束,
UIView *containerView = self.containerView;
UIView *mySubview = [[MySubview alloc] init];
[mySubview setTranslatesAutoresizingMaskIntoConstraints:NO];
[containerView addSubview:mySubview];
NSDictionary *views = …Run Code Online (Sandbox Code Playgroud) 在过去的几天里,我试图使用自动布局约束完成一个相当简单(至少应该是)布局,但没有成功.
我的视图层次结构是:
UIScrollView
- UIView(容器视图)
----- UILabel(Multirow标签)
----- UIWebView
----- UILabel
----- UIButton

所需的功能是根据内容的大小进行扩展的Container视图.为了增加UIWebView的高度,我使用以下代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webView sizeToFit];
}
Run Code Online (Sandbox Code Playgroud)
我尝试了许多不同的约束,最合理的约束是:
1.从第一个标签(事件标题)的superview引脚顶部空间
2.第一个标签和UIWebView的
引脚垂直间距3.其余元素的引脚垂直间距(即UIWebView - UILabel(事件日期)和UILabel(事件日期) - UIButton)
4.容器视图具有低优先级(1)底部垂直空间约束及其超级视图
我得到的结果如下:

UIWebView扩展但不会按下事件日期标签和按钮,而且Container视图也不会展开.
任何帮助将不胜感激.
我想以编程方式更改UITextView的高度约束,因此我将约束设置为我的控制器中的插座,如下所示:
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *descriptionHeightConstraint;
Run Code Online (Sandbox Code Playgroud)
要更改它,我执行以下操作:
self.descriptionHeightConstraint.constant = self.description.contentSize.height;
Run Code Online (Sandbox Code Playgroud)
这是有效的,如果我在viewDidAppear中这样做,但问题是我可以看到高度在显示的视图之后如何变化,这不是非常用户友好所以我尝试在viewWillAppear中进行但是在那里没有工作,高度没有改变.更改约束后调用setNeedsUpdateConstraints也不起作用.
为什么在viewDidAppear中工作而不在viewWillAppear中工作?任何解决方法?
提前致谢!
所以我有代码,它在iOS 7.0上成功运行,但在7.1中没有.我有一个简单的tableview,代码如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
UILabel *label = [[UILabel alloc] init];
label.text = [NSString string];
for (NSInteger i = 0; i < 20; i++) {
label.text = [label.text stringByAppendingString:@"label String "];
}
label.translatesAutoresizingMaskIntoConstraints = NO;
label.numberOfLines = 0; …Run Code Online (Sandbox Code Playgroud) 我UITableViewCell在故事板中定义自定义.我也使用了一些内置的标准样式单元格.我需要设置约束,以便我的自定义单元格与平台定义的单元格匹配(在左侧正确对齐它们的标题).
我注意到,在iOS版本和运行它的设备之间,单元格的左对齐会发生变化.它看起来像左对齐相同的默认单元格separatorInset.
这里是UITableViewCell小号separatorInset几设备和iOS版本:
其他设备(iPhone 6 +,iPad Mini)可能有所不同 - 我没有详尽检查.
您将如何确保自定义单元格和内置单元格的标签左对齐?
是否有一种合理的方法来获取这些插入默认值并在故事板中设置的自动布局约束中使用它们?代码怎么样?
我正在尝试学习自动布局,最后我想在发生这种情况时我正在抓住它.我正在玩原型细胞和标签.我想要一个
titleLbl- 图片中的蓝框moneyLbl- 图片中的绿色框subTitleLbl- 图中的红色框到目前为止我有这个:

我想要实现的是:
正如您所看到的,除了最后一行中的示例之外,这几乎都可以正常工作.我试图对此进行研究,从我可以收集的内容来看,这与内在的内容大小有关.网上有很多帖子推荐设置setPreferredMaxLayoutWidth,我已经在多个地方做了这个titleLbl并且没有任何效果.只有在硬编码时才能180得到结果,但它还在文本顶部/下方的其他蓝色框中添加了空格/填充,大大增加了红色/蓝色框之间的空间.
这是我的约束:
标题:

钱:

字幕:

基于我与其他文本样本一起运行的测试,它看起来确实使用了故事板中显示的宽度,但任何纠正它的尝试都不起作用.
我创建了一个单元格的ivar并用它来创建单元格的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
[sizingCellTitleSubMoney.titleLbl setText:[[tableViewData objectAtIndex:indexPath.row] objectForKey:@"title"]];
[sizingCellTitleSubMoney.subtitleLbl setText:[[tableViewData objectAtIndex:indexPath.row] objectForKey:@"subtitle"]];
[sizingCellTitleSubMoney.moneyLbl setText:[[tableViewData objectAtIndex:indexPath.row] objectForKey:@"cost"]];
[sizingCellTitleSubMoney layoutIfNeeded];
CGSize size = [sizingCellTitleSubMoney.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MATitleSubtitleMoneyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifierTitleSubTitleMoney];
if(!cell)
{
cell = [[MATitleSubtitleMoneyTableViewCell alloc] init];
} …Run Code Online (Sandbox Code Playgroud) 我有时会想要从布局中删除子视图.它不仅应该被隐藏,而且不应该被视为视图"流"的一部分,可以这么说.一个例子:

我正在寻找一种策略来以编程方式隐藏橙色视图.框的布局及其内容是通过自动布局进行的.有两点需要注意:
我最好的建议是在橙色框中添加一个约束,将其高度设置为0.为此,我需要对橙色框内的所有垂直约束使用非必需的优先级.同时,容器应更新用于分隔框的约束的常量.我不太喜欢这种方法,因为橙色盒子类正在定义它的内部约束,并考虑到它的superview的行为.如果橙色框视图反而暴露了一个"崩溃"方法,它本身就添加了0高度约束,也许我可以忍受它.
有更好的方法吗?
在Xcode 6中,我可以使用Editor菜单来固定:
我无法在Xcode 7 beta中找到这样做的方法.如果我按下Storyboard视图中的Align按钮,我会看到:
我认为是同样的事情,但他们都是灰色的.
在Xcode 6中添加这4个约束大概需要20秒,但我花了30分钟把它放在Xcode 7中并且无法弄明白.
autolayout ×10
ios ×8
uiview ×3
ios6 ×2
objective-c ×2
uilabel ×2
uitableview ×2
calayer ×1
cocoa-touch ×1
constraints ×1
height ×1
ios7.1 ×1
iphone ×1
resize ×1
uiscrollview ×1
uistoryboard ×1
xcode7 ×1