2013-09-19 4 views
1

Я новичок в разработке IOS, но у меня проблемы с UIScrollView. Прежде всего, я создал Scroll View в раскадровке и добавилios uiscrollview addubview не работает

@property (retain, nonatomic) IBOutlet UIScrollView *mainScroll; 

в виде контроллера обработчик

В методе viewDidLoad у меня есть этот код:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content. 
    // You should set these. 
    self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 



    self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these 
    self.mainScroll.maximumZoomScale = 2.0; 
    self.mainScroll.zoomScale = 1; 

    UIButton* b = [[UIButton alloc] init]; 
    [b setBounds:CGRectMake(.0f,0.f,100.0,100.0)]; 
    [self.mainScroll addSubview:b]; 



} 

Но ничто не появляется в тренажере. Однако, если я добавлю кнопку на раскадровке в IB, появится кнопка, и я могу прокрутить представление.

P.S. извините за мой язык

+0

Вы используете ARC? Вы должны быть, если вы этого не сделаете. – Fogmeister

+0

убедитесь, что на рамке кнопки. Почему вы установили привязку, но .0f и 0.f - это x и y шнур кнопки. Пожалуйста, проверьте размер рамки. – Subrat

ответ

6

Попробуйте этот код

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setTitle:@"Show View" forState:UIControlStateNormal]; 
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); 
[self.mainScroll addSubview:button]; 
+0

LOL! Великие мысли думают одинаково. – Fogmeister

+1

Да Fogmeister вы правы – Tendulkar

1

bounds и frame не то же самое.

Используйте этот код вместо ...

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setTitle:@"Hello" forState:UIControlStateNormal]; 
button.frame = CGRectMake(0, 0, 100, 44); 
[self.mainScroll addSubview:button]; 
0

Попробуйте это:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setTitle:@"Hello" forState:UIControlStateNormal]; 
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44); 
[self.mainScroll addSubview:button]; 
0

лучший и самый простой способ сделать кнопку из кода:

UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)]; 
    [yourButton setTitle:@"Your Title" forState:UIControlStateNormal]; 

// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image. 
    [yourButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal]; 

// if you set the button's background image, button's title will be visible 
    [yourButton setBackgroundImage:[UIImage imageNamed:@"yourBackgroundImage.png"] forState:UIControlStateNormal]; 

// you can add target to the button, for handle the event when you touch it. 
    [yourButton addTarget:self action:@selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside]; 

// then add the button 
    [self.view addSubview:yourButton]; 


- (void) handleToucUpInside:(id)sender{ 

    // here you can do anything you want, to handle the action 


} 
1

Вам нужно установите размер содержимого прокрутки, например, следующий код.

[self.mainScroll setContentSize:CGSizeMake(width, height)]; 
+0

По какой-то причине для меня добавление subviews UIImageView не будет прокручиваться, пока я это не сделаю. –

Смежные вопросы