我有一个包含两个子视图的内容视图.我已垂直堆叠子视图并将其水平居中(x轴)在内容视图中:
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[buttonView]-[label]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(buttonView, label)]];
Run Code Online (Sandbox Code Playgroud)
现在我想垂直居中(Y轴).我想从buttonView的顶部和标签视图的底部获得相等的空间.
我怎样才能做到这一点?优先考虑的顶部和底部约束?
编辑:
Scott,这是你建议的代码看起来如何:

我尝试在buttonView和标签之间建立间距,-1-但没有帮助.这就是我需要它的样子:

这是我用来实现它的代码:
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-13-[buttonView]-[label]-10-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(buttonView, label)]];
Run Code Online (Sandbox Code Playgroud)
我的约束工作,但硬编码,我宁愿远离,如果可能的话.
因此,在常规类中,您可以覆盖类属性的setter方法:
-(void)setSortBy:(NSString *)sortBy {
// Do other stuff
_sortBy = sortBy;
}
Run Code Online (Sandbox Code Playgroud)
使用它_可以防止方法的无限循环调用它自己.
我试图用NSManagedObject类做类似的事情,但它没有给出使用下划线(_)的选项:
-(void)setHasNewData:(NSNumber *)hasNewData {
// update self.modifiyDate
_hasNewData = hasNewData;
}
Run Code Online (Sandbox Code Playgroud)
给我一个错误,并建议我更换_hasNewData到hasNewData.
这是应该怎么做或它会给我一个无限循环?
我希望它随时更新我NSManagedObject的属性.modifyDatehasNewData
需要向文本框添加向下箭头.决定使用rightViewMode来显示它.它适用于我使用的第一个文本字段,但不会显示在第二个文本字段中.它发生在3个不同的视图中,我需要这样做.
码:
//added to post
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
UIView *rightPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
UIImageView *unitArrowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"prev-arrow.png"]];
unitArrowImage.frame = CGRectMake(0, 0, 20, 20);
[unitArrowImage setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
[rightPaddingView addSubview:unitArrowImage];
//other code
UITextField *seedRateUnitTextField = [[UITextField alloc] initWithFrame:myRect];
seedRateUnitTextField.font = [UIFont fontWithName:textFieldFont size:textFieldFontSize];
seedRateUnitTextField.placeholder = @"Unit";
seedRateUnitTextField.borderStyle = UITextBorderStyleLine;
seedRateUnitTextField.backgroundColor = [UIColor whiteColor];
seedRateUnitTextField.textColor = [UIColor textFieldTextColor];
seedRateUnitTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
seedRateUnitTextField.layer.borderColor = [[UIColor textfieldBorderColor]CGColor];
seedRateUnitTextField.layer.borderWidth = 1.0f;
seedRateUnitTextField.leftView …Run Code Online (Sandbox Code Playgroud) 所以我有一系列自定义对象.我想通过它们找到maxx,maxy,minx和miny值.我需要最大的最大和最小的分钟.下面的代码对我来说很有意义,但是我倾向于从列表中得到一个随机值,我的最大值不会达到最大值或最小值,并且分钟不会得到最小值或最大值.
所以,我从数组中的第一个对象获取第一个值,然后将每个对象值与我当前的maxx,maxy,minx,miny进行比较,以查看对象的值是大于还是小于,如果是,则指定它:
claimCenterBoundary = [dataCenter.claimCenterBoundaryList objectAtIndex:0];
maxx = claimCenterBoundary.maxx;
maxy = claimCenterBoundary.maxy;
minx = claimCenterBoundary.minx;
miny = claimCenterBoundary.miny;
for (int i = 0; i < count; i++)
{
claimCenterBoundary = [dataCenter.claimCenterBoundaryList objectAtIndex:i];
NSLog(@"minx: %@ miny: %@ maxx: %@ maxy: %@", claimCenterBoundary.minx, claimCenterBoundary.miny, claimCenterBoundary.maxx, claimCenterBoundary.maxy);
if (maxx < claimCenterBoundary.maxx)
maxx = claimCenterBoundary.maxx;
if (maxy < claimCenterBoundary.maxy)
maxy = claimCenterBoundary.maxy;
if (minx > claimCenterBoundary.minx)
minx = claimCenterBoundary.minx;
if (miny > claimCenterBoundary.miny)
miny = claimCenterBoundary.miny;
}
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
count: 8
minx: -98.9139404296875 miny: 48.51737594604492 …Run Code Online (Sandbox Code Playgroud) 所以我需要绘制2条不同的线条.通过另一个帖子,我想出了如何重新绘制一条线.我的问题是,如果我想绘制2行,我需要有2个代码段来做吗?或者有没有办法绘制n行?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"drawRect called");
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
CGContextStrokePath(context);
}
Run Code Online (Sandbox Code Playgroud)
履行
draw2D *myCustomView = [[draw2D alloc] init];
myCustomView.startPoint = CGPointMake(0, 0);
myCustomView.endPoint = CGPointMake(300, 300);
myCustomView.lineWidth = 5;
myCustomView.lineColor = [UIColor redColor];
myCustomView.frame = CGRectMake(0, 0, 500, 500);
[myCustomView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myCustomView];
[myCustomView setNeedsDisplay];
myCustomView.endPoint = CGPointMake(100, 100);
myCustomView.lineColor = [UIColor orangeColor];
[myCustomView setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)
这只会重新划线.我会为我想要的每一行重复drawRect块吗?所以,如果我想要两条线,我必须这样做:
- (void)drawRect:(CGRect)rect
{
//line1
CGContextRef context1 = UIGraphicsGetCurrentContext();
NSLog(@"drawRect called"); …Run Code Online (Sandbox Code Playgroud) 我有一个充满按钮的UIView*buttonView.我需要更新我的列表并重新填充我的buttonView.我实施了:
if ([[self.buttonView.subviews objectAtIndex:i] isKindOfClass:[UIButon class]]
[[self.buttonView.subviews objectAtIndex:i] removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)
但它失败了,它不会删除所有的按钮(我的测试有8个按钮,它删除了每个其他按钮: - ?)
然后我尝试了:
for(UIView *subview in self.buttonView.subviews)
{
if([subview isKindOfClass:[UIButton class]])
[subview removeFromSuperview];
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好.
两个循环不应该完成同样的事情吗?
我猜有些东西我不知道快速枚举可以解释这个吗?
我有一个名为Layer的对象,它有一些属性和一些方法.
我需要将Layer传递给第二个视图控制器:
SecondVC *view = [self.storyboard instantiateViewControllerWithIdentifier:@"2VC"];
view.Layer = [[Layer alloc] initWithMapLayer:self.Layer];
view.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:navController animated:YES];
Run Code Online (Sandbox Code Playgroud)
在SecondVC中,我可能会更改属性.然后我通过委托返回Layer对象;
-(void)done
{
[self.delegate returnLayer:self.layer];
[self dismissModalViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是我将指针传递给我的第一个视图控制器的Layer对象,当我更新第二个视图控制器中的Layer时,我的第一个视图控制器的Layer对象也正在更新.
因为这个,我无法判断它是否已被更改(如果有,我需要运行一些代码).
我如何创建我的Layer对象的副本并将其传递给我的第一个视图控制器的Layer对象而不是指针?
编辑:
我尝试使用第二个init方法:
-(id)initWithLayer:(Layer *)Layer
{
if (self = [super init])
{
self.call = [[FunctionCall alloc] init];
self.HUD = [[MBProgressHUD alloc] init];
self.Layers = [[NSMutableDictionary dictionaryWithDictionary:Layer.Layers] copy];
self.nameList = [[NSArray arrayWithArray:Layer.nameList] copy];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
没有成功.
EDIT2:
试着
Layer *copyLayer = [self.myLayer …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个面板来显示一些信息.我无法按照自己的意愿将autolay运行起来.
这是我正在尝试做的事情:

我有一个headerView,一个UIImageView和5个标签.我希望标签垂直排列,我希望它们与UIImageView有8个间距.我可以很容易地垂直排列它们:
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-[acresLabel]-[addedLabel(==acresLabel)]-[typeLabel(==acresLabel)]-[zonesLabel(==acresLabel)]-[sceneLabel(==acresLabel)]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(headerView, acresLabel, addedLabel, typeLabel, zonesLabel, sceneLabel)]];
Run Code Online (Sandbox Code Playgroud)
但让它们排列在图像视图之外是一种繁琐/冗长的方式:
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[acresLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, acresLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[addedLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, addedLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[typeLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, typeLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[zonesLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, zonesLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[sceneLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, sceneLabel)]];
Run Code Online (Sandbox Code Playgroud)
似乎是一种更好的方法,然后为图像视图的每个标签设置约束.我尝试添加NSLayoutFormatAlignAllCenterX到第一个约束,但它将它们全部设置为内容视图的中心X,不会将标签的中心X相互对齐.NSLayoutFormatAlignAllBaseline在垂直使用时给出错误和崩溃.
我将不得不再次为数据做这个(英亩:'价值').
我有一个字典作为另一个字典的值:
10 = {
createdate = "2012-03-20 15:04:09.125177-05";
}
Run Code Online (Sandbox Code Playgroud)
我想为内部字典设置一个值,而不必将它拉出来,但是
[NSDicionary setObject:forKey:]
Run Code Online (Sandbox Code Playgroud)
只有一个深入.我可以添加另一个键吗?我试过了
[[NSDicionary setObject:forKey:]forKey:]
Run Code Online (Sandbox Code Playgroud)
但xCode不喜欢这样.
有任何想法吗?
需要通过以下方式制作NSMutableDictionary的副本:
NSMutableDictionary *newScoutingEventDictionary = [[[NSMutableDictionary alloc] initWithDictionary:self.scoutingEvent copyItems:YES] mutableCopy];
Run Code Online (Sandbox Code Playgroud)
但是当我尝试改变其中的数组时:
[[newScoutingEventDictionary objectForKey:@"myArray"] replaceObjectAtIndex:i withObject:appendedEntry];
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误:
-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x964d650
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x964d650'
Run Code Online (Sandbox Code Playgroud)
如果我试图改变原来的NSMutableDictionary:
[[self.scoutingEvent objectForKey:@"myArray"] replaceObjectAtIndex:i withObject:appendedEntry];
Run Code Online (Sandbox Code Playgroud)
它运行得很好.为什么我的复制版本打破了它?