gpt4 book ai didi

ios - 使用 NSLayoutConstraint 的 Superview 中 UIPageControl 的编程垂直对齐

转载 作者:可可西里 更新时间:2023-11-01 03:52:12 25 4
gpt4 key购买 nike

到目前为止,我能够避免使用自动布局,因此对于这个(可能)愚蠢的问题深表歉意。

  • 我有一个以编程方式构建的 ViewController(例如,没有 IB)。
  • 我有一个 UIPageControl 对象,它位于 viewController View 的 View 层次结构中(例如,addSubview:)
  • 相反,我需要将 UIPageControl 对象与 UIViewController 主视图的底部边缘对齐。
  • 我希望 UIPageControl 水平居中,但距离底部边缘 20-40 像素。

这是我创建 UIPageControl 并显示它的一些示例代码,但根本不在正确的位置。

self.pageControl = [[UIPageControl alloc] init];
self.pageControl.numberOfPages = ...;
self.pageControl.otherAttributes = ...;
[self.view addSubview:[self pageControl]];

UIPageControl *pageControl = [self pageControl];
UIView *view = [self view];
NSDictionary *constaintDictionary = NSDictionaryOfVariableBindings(view, pageControl);


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:pageControl
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[pageControl]-20-[view]"
options:0
metrics:nil
views:constaintDictionary]];

就像我说的,我对自动布局真的很陌生,所以我不确定我是否正确地实现了这些方法。

感谢您的提前协助。

最佳答案

好吧,我知道怎么做了。花了太多时间,但在我通读了Visual Format Language之后再次查看文档,我想到了这个:

UIPageControl *pageControl = [self pageControl];

[pageControl setTranslatesAutoresizingMaskIntoConstraints:NO];

[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[pageControl]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(pageControl)]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[pageControl]-40-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(pageControl)]];

关于ios - 使用 NSLayoutConstraint 的 Superview 中 UIPageControl 的编程垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19822972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com