红色记录栏扭曲视图

时间:2014-09-24 04:05:35

标签: ios objective-c iphone

我有点时间搞清楚这一点。

我点击标签栏的其中一个标签时会出现模态视图。在占据整个屏幕的新视图中,有一个用户点击以开始录制音频的按钮,当他们完成录制音频时,或者如果他们取消录制,则取消模态视图。

问题在于,一旦他们开始录制,该红色录制栏应该出现在这个新视图上。但事实并非如此。一旦该视图被解除,在下一个呈现的视图中(一个表视图,可以在其中一个选项卡中找到),您可以看到顶部的红色记录栏一瞬间消失但它消失了但是它将标签栏推到屏幕下方并隐藏标签栏的一部分,您可以在下面的第三个屏幕截图中看到。

Modal view pops up - recording is currently in progress, but red recording indicator is not showing at the top 弹出模态视图 - 录制当前正在进行中,但红色录制指示器未显示在顶部

Right when the recording is done and the view is about to disappear, the red bar appears 当录制完成并且视图即将消失时,红色条出现

And once the view disappears and we're left with the table view that lives in one of the tabs, the tab bar is shoved down past the bottom of the screen :( 一旦视图消失并且我们留在其中一个选项卡中的表视图中,标签栏就会被推到屏幕底部:(它应该看起来像这样(选择了第四个选项卡): enter image description here

我的问题:

1)我做错了是什么导致红色录音条没有出现在模态视图中?

2)是否有办法从屏幕截图中刷新此视图,以便在显示时正确调整大小?

这是代码。我删除了一些不处理视图的非重要内容。

@interface AudioViewController ()

@end

@implementation AudioViewController

@synthesize fileData;

UILabel *countdownLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];
    self.recipients = [[NSMutableArray alloc] init];
}


- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    self.audioPicker = [[UIViewController alloc] init];
    self.audioPicker.view.backgroundColor = [UIColor yellowColor];

    self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];
    PFQuery *query = [self.friendsRelation query];
    [query orderByAscending:@"username"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
        else {
            self.friends = objects;
            [self.tableView reloadData];
        }
    }];

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancelBtn.frame = CGRectMake(50.0, 200.0, 200.0, 200.0);
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
    [self.audioPicker.view addSubview:cancelBtn];
    cancelBtn.center = CGPointMake(self.view.center.x, 400);
    [cancelBtn addTarget:self action:@selector(exitRecordingScreen) forControlEvents:UIControlEventTouchUpInside];

    UIButton *recordBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    recordBtn.frame = CGRectMake(50.0, 50.0, 200.0, 200.0);
    recordBtn.titleLabel.font = [UIFont systemFontOfSize:50];
    [recordBtn setTitle:@"Record" forState:UIControlStateNormal];
    recordBtn.center = CGPointMake(self.view.center.x, 100);
    [self.audioPicker.view addSubview:recordBtn];

    if ([self respondsToSelector:@selector(timeout)]) {
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
    } else {
        NSLog(@"Error: missing selector");
    }

    AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setCategory:AVAudioSessionCategoryPlayAndRecord
             withOptions:AVAudioSessionCategoryOptionDuckOthers
                   error:nil];

    if (!fileData) {
        [self presentViewController:self.audioPicker animated:NO completion:nil];
        NSLog(@"File data: %@", fileData);
        [recordBtn addTarget:self action:@selector(startRecordingAudio) forControlEvents:UIControlEventTouchUpInside];
    } else {
        NSLog(@"Existing File data: %@", fileData);
    }
}

- (void) timeout {

    [self.navigationController popViewControllerAnimated:YES];
}

# pragma mark - Audio Recording Methods

////////
// Removed some stuff here that is not manipulating views
////////

- (void) stopRecordingOnAudioRecorder:(AVAudioRecorder *)paramRecorder{
    /* Just stop the audio recorder here */
    [paramRecorder stop];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder
                           successfully:(BOOL)flag{
    if (flag) {
        NSLog(@"Stopped recording process");
        NSError *playbackError = nil;
        NSError *readingError = nil;
        fileData = [NSData dataWithContentsOfURL:[self audioRecordingPath]
                              options:NSDataReadingMapped
                                error:&readingError];

        self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData
                                                         error:&playbackError];

        if (self.audioPlayer != nil) {
            self.audioPlayer.delegate = self;

            //Prepare and start playing
            if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play]) {
                NSLog(@"Started playing recorded audio");
            } else {
                NSLog(@"Couldn't play recorded audio");
            }


        } else {
            NSLog(@"Failed to create audio player");
        }
    } else {
        NSLog(@"Stopping audio recording failed");
    }
    self.audioRecorder = nil;
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
                       successfully:(BOOL)flag{
    if (flag){
        NSLog(@"Audio player stopped correctly.");
    } else {
        NSLog(@"Audio player did not stop correctly.");
    }
    if ([player isEqual:self.audioPlayer]){
        self.audioPlayer = nil;
    } else {
        /* This is not the player */
    }
}

# pragma mark - TableView methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.friends count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFUser *user = [self.friends objectAtIndex:indexPath.row];
    cell.textLabel.text = user.username;

    // makes sure checkmark isn't reused if user didn't explicitly select name
    if ([self.recipients containsObject:user.objectId]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)reset {
    self.audioFile = nil;
}

// User hits "Cancel" button
-(void)exitRecordingScreen {
    [self reset];
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    [self.tabBarController setSelectedIndex:0];
    NSLog(@"exit recording screen button pressed");

}

- (IBAction)send:(id)sender {
    if (self.audioFile == nil) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please try again." message:@"Please record audio again to share." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertView show];
        [self presentViewController:self.audioPicker animated:NO completion:nil];
    } else {
        [self uploadMessage];
        [self.tabBarController setSelectedIndex:0];
    }
}
// Cancel sending recorded file
- (void)cancel:(id)sender {
    fileData = nil;
    [self reset];
    [self.tabBarController setSelectedIndex:0];
}

@end

抱歉文字墙和长度。我真的很难过。

1 个答案:

答案 0 :(得分:1)

解决方案:您必须为UITabBarController重置帧。 1.最初UITabBarController的帧将是(0,0,screenWidth,screenHeight)。 但是当这个记录红条出现时它会变成(0,20,screenWidth,screenHeight) 3.在这里你应该改变UITabBarController的高度

CGRect changedFrame = objMainTabBarController.view.frame;   changedFrame.size.height = [UIScreen mainScreen] .bounds.size.height - CGRectGetMinY(changedFrame);   objMainTabBarController.view.frame = changedFrame;

那是......