Silver Challenge - another solution - Tab Bar kept


Here is another solution for the silver challenge. I did not change the code in BNRAppDelegate in order to keep the tab bar as I want to. So I updated the code in the BNRHyponosisViewController.m as following:

#import "BNRHynosisViewController.h"
#import "BNRHypnosisterView.h"

@interface BNRHynosisViewController () <UIScrollViewDelegate>

@property (strong, nonatomic) BNRHypnosisterView *hypnoView;


@implementation BNRHynosisViewController

- (void)loadView
    self.view = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    CGRect frame = [UIScreen mainScreen].bounds;
    CGRect screenRect = frame;
    CGRect scrollRect = screenRect;
    scrollRect.size.width *= 2.0;
    scrollRect.size.height *= 2.0;
    // Create a screen sized scroll view and add it to the view window
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
    [self.view addSubview:scrollView];
    // Create a subview of scrollView
    self.hypnoView = [[BNRHypnosisterView alloc] initWithFrame:scrollRect];
    [scrollView addSubview:self.hypnoView];
    // Set up the content size
    scrollView.contentSize = scrollRect.size;
    // Set up the zoom scale
    scrollView.minimumZoomScale = 1; // could be smaller than 1
    scrollView.maximumZoomScale = 10;
    // Making a delegate
    scrollView.delegate = self;
    // Center the view =;
    self.view = scrollView;

// Challenge part, without this part, you can only scroll, but not to zoom
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    return self.hypnoView;

- (instancetype)initWithNibName:(NSString *)nibNameOrNil
                         bundle:(NSBundle *)nibBundleOrNil
    self = [ super initWithNibName:nibNameOrNil
    if (self) {
        // Set the tab bar item's title
        self.tabBarItem.title = @"Hypnotize";
        // Create a UIImage from a file
        UIImage *i = [UIImage imageNamed:@"Hypno.png"];
        // Put that image on the tab bar item
        self.tabBarItem.image =i;
    return self;

- (void)viewDidLoad
    // Always call the super implementation of viewDidLoad
    [super viewDidLoad];
    NSLog(@"BNRHyponosisViewController loaded its view");



does your textfield and the segmented control show up?


I’ve removed the textfield part. The segmented control (I assume you mean “tab bar” controller) did show up.


I just tested that if I want to view the text field, I can add the following codes into BNRHyposistViewController.m

@interface BNRHynosisViewController () <UITextFieldDelegate>


and the following in the above method " - (void)loadView "

    CGRect textFieldRect = CGRectMake(40, 70, 240, 30);
    UITextField *textField = [[UITextField alloc] initWithFrame:textFieldRect];
    // Setting the border style on the text field will allow us to see it more easiyly
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [scrollView addSubview:textField];
    textField.placeholder = @"Hypnotize me";
    textField.returnKeyType = UIReturnKeyDone;
    //    textField.secureTextEntry = @"Yes";
    textField.delegate =self;

and any other codes related to textField written before, such as

- (BOOL)textFieldShouldReturn:(UITextField *)textField

    [self drawHypnoticMessage:textField.text];
    textField.text = @"";
    [textField resignFirstResponder];
    return YES;


Hi I used you stuff for reference but added a few features and though it might be useful for other people doing this I added a property attribute for the textbox and the scrollview so i could control them.

@property(strong,nonatomic) UITextField *textField; @property(strong,nonatomic) UIScrollView *scrollView;

Then I added the scroll view protocol scrollViewDidScroll which then makes the text box position stay fixed wherever you move

[code]- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

CGPoint point = CGPointMake(scrollView.contentOffset.x +self.textField.bounds.size.width/2 +40, scrollView.contentOffset.y +self.textField.bounds.size.height/2 +70); = point;


I also added the following to the hypnotize text so that it would display on the full screen

[code] int width = (int)(self.scrollView.contentSize.width - messageLabel.bounds.size.width);
int x = arc4random_uniform(width);

    int height = (int)(self.scrollView.contentSize.height  - messageLabel.bounds.size.height);
    int y = arc4random_uniform(height);
    CGRect frame = messageLabel.frame;
    frame.origin = CGPointMake(x,y);
    messageLabel.frame = frame;
    [self.view addSubview:messageLabel];[/code]