gpt4 book ai didi

iphone - UIScrollView 在关闭模态视图 Controller 后立即向下移动 44px(等于导航栏高度)

转载 作者:行者123 更新时间:2023-12-01 16:48:03 25 4
gpt4 key购买 nike

我有一个带有导航栏的 UIViewController。我的 UIScrollView 包含 UIButtons 列表以编程方式添加。我使用 AutoLayout 来添加 ScrollView 及其按钮列表,如下所示。

在显示和关闭模态视图之前,它可以完美运行 .模态视图关闭后, ScrollView 中的按钮列表向下移动 44px(等于导航栏高度)。
当我尝试删除导航栏时,一切正常。

我搜索了以下问题,但不是我的情况:

Contents of UIScrollView shifted after dismissing ModalViewController

Dismiss modal view changes underlying UIScrollView

我使用 AutoLayout 添加 uiscrollview 的代码:

UIScrollView* scrollView = [UIScrollView new];
[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

//calculate contentSize based on the button count
int buttonCount = 10;
CGSize contentSize = CGSizeMake(ICON_X*2 + (ICON_WIDTH + ICON_SPACE) * buttonCount - ICON_SPACE, ICON_HEIGHT);

//calculate trailing offset for the first button
CGFloat trailingOffset = contentSize.width - (ICON_X + ICON_WIDTH);

//add list of button to the scrollview
for (int i = 0; i < buttonCount; i++) {

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];

//set button image
//set button target

//set tag
button.tag = i;

//add to the scroll view
[scrollView addSubview:button];

//calculate the button's frame using AutoLayout
NSDictionary *dict = NSDictionaryOfVariableBindings(button);
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-%g-[button(%d)]-%g-|", ICON_X + (ICON_WIDTH + ICON_SPACE)*i, ICON_WIDTH, trailingOffset] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat: @"V:|-%g-[button(%d)]-%g-|", ICON_Y, ICON_HEIGHT, ICON_Y] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];

//update the trailing offset for the next button
if (i == buttonCount - 2) //the next one is the last button
trailingOffset = ICON_X;
else
trailingOffset -= (ICON_SPACE + ICON_WIDTH);
}

//add the scrollView to self.viewIcon
//self.viewIcon is a UIView which was added as a subview of self.view in the storyboard using AutoLayout
[self.viewIcon addSubview:scrollView];

//calculate the scrollview's frame using AutoLayout
//contraint autolayout
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(1)-[scrollView(58)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)];
NSArray *horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)];
[self.viewIcon addConstraints:verticalConstraints];
[self.viewIcon addConstraints:horizontalContraints];

请注意, ScrollView 被添加到 self.viewIcon,它是使用 AutoLayout 在 Storyboard 中作为 self.view 的 subview 添加的

谢谢

最佳答案

我在iOS 6上也有同样的问题。这个问题似乎在iOS7GM上得到了解决。

在这里寻找解决方案:Shifting view after displaying modal - possibly AutoLayout related

关于iphone - UIScrollView 在关闭模态视图 Controller 后立即向下移动 44px(等于导航栏高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18427510/

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