【问题标题】:Why is the UIImageView not hiding when using a UISegmentedControl?为什么使用 UISegmentedControl 时 UIImageView 不隐藏?
【发布时间】:2015-01-26 04:36:09
【问题描述】:

我有一个 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];
    }
}

【问题讨论】:

    标签: ios objective-c uiscrollview uisegmentedcontrol


    【解决方案1】:

    您应该从- (IBAction)segmentedControl:(UISegmentedControl *)sender 方法中删除[self setScroller];,并将其放入-(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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的问题可能是您创建了很多“imageView”实例 为了解决您的问题,我建议:

          -(void)setScroller
          {
              CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
              [self.scrollView setContentSize:scrollableSize];
      
      
              if(self.imageView == nil) {
              self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
      
          [self.scrollView addSubview:self.imageView];
              }
      
      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.imageView setNeedsDisplay];
      
              [self.imageView setImage:[UIImage imageNamed: @"test.png"]];
          }
      

      我只把 self.imageView 的 Alloc + addSubView 放到了 if 中, 其余的都在外面,

      现在可以了

      【讨论】:

      • 感谢您的建议,这几乎可行,但现在我的图像是黑色的,而不是我的实际图像
      • 那很奇怪,尝试添加行:[self.imageView setNeedsDisplay];在 if 之外(我编辑答案)
      • 可惜还是没有图片:(
      • 只是为了更好地理解问题,“黑色”方块是否响应隐藏和未隐藏? (我想确定问题是UIImageView没有显示,还是UIImageView中的Image没有显示)
      • 使用您新添加的代码,图像现在出现在不同的响应之间,但是最后一行代码导致图像的高度不是超级视图的高度,换句话说,图像的高度较小,但我希望它是 self.view.frame.size.height
      猜你喜欢
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 2014-10-17
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多