mi primera vista personalizada no se desplaza

I had a text view and i was taking chat that came in from the internet and was printing it but i wanted more colors than a textview had to format the chat.

I replaced the textview with a scrollview and in viewdidload i attempted ( seemed to work) to attach my view class to it.

- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _myconsoleview = [[ConsoleView alloc] initWithFrame:[self._scview bounds]];
    [_myconsoleview setBackgroundColor: [UIColor yellowColor]];
    [self._scview addSubview:_myconsoleview];
    [self._scview setNeedsDisplay];

well as text got to the bottom it didn't scroll ( i tried dragging the text are with the mouse and looked for scroll bars).

so this example core text program says oh thats easy to get scrolling. just make my custom view (ConsoleView) inherit the scroll view itself. So i try this and add this now:

@interface ConsoleView :UIScrollView<UIScrollViewDelegate>

still no scrolling. no scroll bars. no indication and i'm using the ipad simulator so far, of any kind of scrolling. In ConsoleView i want to do something very polished eventually but for now i'm just printing a single string. But the string gets longer as i get more text as i'm appending to it.

i tried this in my sendtext method. this hits when they hit a button to send the text they type in this textfield to the server:

   [myconsoleviewid addNewText:newText];
    [_scview scrollRectToVisible:[_scview bounds] animated: TRUE];
    [_scview setNeedsDisplay];

I honestly am not sure i can claim the above would work but i figured something might work. any ideas? I'm fairly new to ios programming having started about 2 months ago and worked on it off and on so far. In java or android there doesn't seem to be much more to getting like a console to scroll than to add it to some scroll control. But IOS is proving trickier.


edit. this is my draw rectangle which is suppose to print the string which gets longer and longer. its in consoleview:

- (void)drawRect:(CGRect)rect
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);    CGMutablePathRef path = CGPathCreateMutable(); //1
    CGPathAddRect(path, NULL, self.bounds );

    CTFramesetterRef framesetter =
    CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attString); //3
    CTFrameRef frame =
                             CFRangeMake(0, [attString length]), path, NULL);

    CTFrameDraw(frame, context); //4

    CFRelease(frame); //5

preguntado el 31 de julio de 12 a las 15:07

2 Respuestas

The ScrollView's contentSize must be larger than the ScrollView's frame in order to allow scrolling. Set this property correctly and you should be fine.

Respondido 31 Jul 12, 15:07

if i set the overall scroll view content size to like 20000 vertical, my yellow background stops at one page so this seems wrong. If i set my view which is a scroll view to like 2000 vertical, it scrolls, i see my yellow background my view has persist, but text still stops showing after the last line of page one. - LinternaMike

I saw I had not accepted an answer. I wound up changing the core text code, but with the scrolling issue still, I found that this was the trick, contentsize being at least one larger, accepting this answer. - LinternaMike

Necesitas configurar scrollView.contentSize with proper size of scrollable area.

scrollView.contentSize = CGSizeMake(640, 320); // if you are in full screen and have two view one to other in horizontal

// for vertical scrolling you need to do

scrollView.contentSize = CGSizeMake(320, 640);

This examples are only for one way scrolling horizontal or vertical if you set the contentSize bigger than view frame size if will be scrollable.

Respondido 31 Jul 12, 15:07

if i'm simply printing a long string how do i know the size? String just has text and it has \n characters in it. Also at this point i've got two scroll views i guess. the one i dragged on to the storyboard and hte view i attached to it which is a scrollview too. does it matter which one i set the size of? - LinternaMike

You need to set the contentSize of the scrollView and to be scrollable contentSize must be bigger that UIView frame size. Because that scrollView is in UIView. - h4cky

i just want vertical scrolling so i added _scview.contentSize = CGSizeMake(320, 1040);. i did this to my overall scroll view. i made my view i attached to it just a UIView. well i have a yellow background on my view and when i scroll now the background changes to white and there is no text . did i attach the view wrong? when i set the contents size of my consoleView before i made it a uiview, i also got some scrolling but still no new text printed below the initial stop point of text or one screen. - LinternaMike

Your ViewController have view, after that you add your scrollView to that view. What you use about displaying the text UITextView? - h4cky

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.