gpt4 book ai didi

ios - 为什么使用 UISegmentedControl 时 UIImageView 不隐藏?

转载 作者:行者123 更新时间:2023-11-29 02:21:01 26 4
gpt4 key购买 nike

我有一个 UISegmentedControl,其中第一个控件将在 UITextView 中显示文本,而第二个控件显示可滚动的 UIImageView。

在初始启动时,如果我切换到第二个控件,图像显示并切换回第一个控件,图像消失,UITextView 显示。

但是,当我第二次切换到第二个控件并切换回第一个控件时,图像仍然存在,我无法再显示 UITextView。

我的代码将其设置为第一个控件中隐藏图像和显示文本的位置,反之亦然。

为什么第一次可以,但第二次切换控件时就不行了?

我做错了什么?

谢谢

-(void)viewDidLoad
{
self.scrollView.delegate = self;

self.textView.text = @"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";

self.textView.hidden = NO;
}

-(void)setScroller
{
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];

self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);

self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
//self.scrollView.zoomScale = 1.0;
[self.scrollView addSubview:self.imageView];
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}

- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display apppropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;

[self setScroller];
}
}

最佳答案

你应该删除 [self setScroller];来自 - (IBAction)segmentedControl:(UISegmentedControl *)sender方法,并将其放入-(void)viewDidLoad反而。您正在调用 [self setScroller];每次切换到第二段时。

您的代码应如下所示:

-(void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
[self setupScroller];
}

-(void)setupScroller
{
// Set contentSize
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.contentSize = scrollableSize;

// Add textView
self.textView.text = @"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";
self.textView.hidden = NO;

// Add ImageView
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.imageView.hidden = YES;
[self.scrollView addSubview:self.imageView];

// Configure Zoom Scales and backgroundColor
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}

- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display appropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;
}
}

关于ios - 为什么使用 UISegmentedControl 时 UIImageView 不隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28135369/

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