简体   繁体   English

如何水平居中滚动视图的内容?

[英]How to horizontally center the content of the scroll view?

In viewDidLoad I'm adding the buttons to the scroll view within the loop as follows: 在viewDidLoad中,我将按钮添加到循环内的滚动视图中,如下所示:

for (int i = 0; i < self.topics.count; i++) {
    id obj = self.topics[i];
    if ([obj isKindOfClass:[TRBTaxonomy class]]) {
        TRBTaxonomy *topic = obj;

        UIButton *prevTopicButton = nil;
        if (i > 0) {
            prevTopicButton = buttons[i-1];
        }

        //Create topic divider
        UILabel *topicsDividerLabel =[[UILabel alloc] init];
        [topicsDividerLabel setBackgroundColor:[UIColor lightGrayColor]];
        [topicsDividerLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

        UIButton *topicButton = [UIButton buttonWithType:UIButtonTypeSystem];
        topicButton.translatesAutoresizingMaskIntoConstraints = NO;
        [topicButton setTitle:topic.name forState:UIControlStateNormal];
        [topicButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        topicButton.titleLabel.font = [UIFont fontWithName:(NSString*)[[TRBUtils fonts] valueForKey:@"TopicsBarFontName"] size:15];
        topicButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        [topicButton addTarget:self action:@selector(topicTapped:) forControlEvents:UIControlEventTouchUpInside];

        [self.scrollView addSubview:topicButton];
    }
}

Then, at the end of the loop I try to get the width of the scroll view content to place it in the center-horizontal within the scroll view: 然后,在循环结束时,我尝试获取滚动视图内容的宽度,以将其放置在滚动视图内的水平中心位置:

CGFloat newContentOffsetX = (self.scrollView.contentSize.width/2) - (self.scrollView.bounds.size.width/2);
self.scrollView.contentOffset = CGPointMake(newContentOffsetX, 0);

but at this point self.scrollView.contentSize.width returns 0 但此时self.scrollView.contentSize.width返回0

When I get the content from viewDidAppear there is a content and everything works fine but there is a visible blink from moving the scroll view content from the left to the center. 当我从viewDidAppear获取内容时,有一个内容,并且一切正常,但是从左到中心滚动视图内容出现了可见的闪烁。

How to get rid of this blink? 如何摆脱这种眨眼? Why self.scrollView.contentSize.width is zero on viewDidLoad and viewWillAppear but not in viewDidAppear? 为什么在viewDidLoad和viewWillAppear上将self.scrollView.contentSize.width设为零,但在viewDidAppear上将self.scrollView.contentSize.width设为零?

Why self.scrollView.contentSize.width is zero on viewDidLoad and viewWillAppear but not in viewDidAppear? 为什么在viewDidLoad和viewWillAppear上将self.scrollView.contentSize.width设为零,但在viewDidAppear上将self.scrollView.contentSize.width设为零?

When method viewDidLoad and viewWillAppear are called the view just hasn't appeared. 调用方法viewDidLoadviewWillAppear ,视图尚未出现。 So the contentSize is 0 shouldn't be strange. 所以contentSize为0并不奇怪。

How to get rid of this blink? 如何摆脱这种眨眼?

try to use this method 尝试使用这种方法

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

CGFloat newContentOffsetX = (self.scrollView.contentSize.width/2) - (self.scrollView.bounds.size.width/2);
[self.scrollView setContentOffset:CGPointMake(newContentOffsetX, 0) animated:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM