到目前为止,我能够避免使用自动布局,因此(可能)愚蠢的问题道歉。
addSubview:
)这里有一些示例代码,它创建了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]];
就像我说的那样,我对自动布局真的很陌生,所以我不确定我是否正确地实现了这些方法。
感谢您提前获得的帮助。
答案 0 :(得分:6)
好吧,我想出了怎么做。花了很多时间,但在我再次阅读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)]];