简体   繁体   中英

black bar appearing above tab bar

I'm new in iOS programming and couldn't solve the following bug in my app:

The storyboard on Xcode 5.0.2 is used to create the app screens. A simple Tab View Controller is the main screen in the app. Each tab bar item is linked to a view controller via a navigation controller. One of the view controllers (S1) contains a button that triggers a segue programmatically after recording audio for a few seconds. The segue transitions to another screen (S2) which sometimes contains a black bar above the tab bar.

Edit

More details about the bug:

  1. Screen S2 can be reached through another screen (S3) in the application. However, the black bar does never appear on S2 when it is opened through S3. The bar only appears on S2 when it is accessed through S1. Therefore, I think it is more related with what I do in S1 (eg audio recording or sleepForTimeInterval , please see below the code).

  2. The black bar disappears after I change the orientation of the device.

  3. No layout constraint is set in S1 or S2.

  4. This bug is observed on iPhone 4, 4S and 5. Did not try on 5S.

And here is the implementation of S1:

@interface S2ViewController () <AVAudioPlayerDelegate, AVAudioRecorderDelegate>

@property (nonatomic, strong) AVAudioRecorder *audioRecorder;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic) IBOutlet UIButton *btnRecord;
@property Parameters* parameters;
@property BOOL permissionGranted;

@end

@implementation STVIdentifyViewController

- (IBAction) recordAudio:(UIButton *)sender
{
    [self startRecordingAudio];
}

- (void) startRecordingAudio
{
    NSError* error = nil;
    NSURL* audioRecordingURL = [self audioRecordingPath];

    self.audioRecorder = [[AVAudioRecorder alloc]
                          initWithURL:audioRecordingURL
                          settings:[self audioRecordingSettings]
                          error:&error];

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

        if ([self.audioRecorder prepareToRecord] && [self.audioRecorder record])
        {
            [self performSelector:@selector(stopRecordingOnAudioRecorder:)
                       withObject:self.audioRecorder afterDelay:5.0];
        }
    }
}

- (void) stopRecordingOnAudioRecorder:(AVAudioRecorder *)paramRecorder
{
    [paramRecorder stop];
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    if (flag)
    {
        [self doSomeProcessingOnTheAudio];
    }

    self.audioRecorder = nil;
}

- (void) doSomeProcessingOnTheAudio
{
    [NSThread sleepForTimeInterval:1.0];

    AnotherClass* ACInstance = [[AnotherClass alloc] init];
    self.parameters = [ACInstance run];

    if (self.parameters)
    {
        [self performSegueWithIdentifier: @"s1tos2" sender: self];
    }
}

- (NSDictionary *) audioRecordingSettings
{
    return @{
             AVFormatIDKey : @(kAudioFormatLinearPCM),
             AVSampleRateKey : @(11025.0f),
             AVNumberOfChannelsKey : @1,
             };
}

- (NSURL *) audioRecordingPath
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSURL *docFolderUrl = [fileManager URLForDirectory:NSDocumentDirectory
                                                    inDomain:NSUserDomainMask
                                           appropriateForURL:nil
                                                      create:NO
                                                       error:nil];
    return [docFolderUrl URLByAppendingPathComponent:@"recording"];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"s1tos2"])
    {
        S2ViewController* s2vc = (S2ViewController*) segue.destinationViewController;
        s2vc.parameters = self.parameters;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    AVAudioSession *session = [AVAudioSession sharedInstance];

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

    [session requestRecordPermission:^(BOOL granted)
    {
        self.permissionGranted = granted;
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

It should be auto layout problem. Check the view's bottom constraint where bar appears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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