iOS UIScrollbar mit Autolayout

Ich kämpfe gerade mit iOS. Ich habe eine Scrollview in der ein paar Labels sind und diese können zum Teil viel Text enthalten. Aber die Scrollview verhindert dass die Texte umbrechen und scrollt statt dessen einfach zur Seite.
Ohne Scrollview funktioniert es problemlos, ich hab das alles per Autolayout gebaut (kein UI Builder)
Was für Möglichkeiten kennt ihr da?

Ohne das jetzt absegnen zu können hab ich das bei Google gefunden:


CGSize scrollableSize = CGSizeMake(320, myScrollableHeight);
[myScrollView setContentSize:scrollableSize];

Im Prinzip muss man die contentSize angeben. Das 320, myScrollableHeight wäre dann die Breite bzw Höhe deiner UI.
Und einen Wert macht man fix. Also in diesem Fall 320 pixel (Ist ein älteres Beispiel glaub ich, darum die kleine Horizontale Auflösung).

ne das hatte ich auch schon probiert aber das funktioniert nicht

Hast du numberOfLines auf 0 gesetzt und den lineBreakMode dementsprechend gesetzt?

Jo wie gesagt ohne ScrollView ist alles wie es sein soll, mit passt es nicht mehr

hm, dann würde ich fast behaupten, an deinem AutoLayout stimmt irgendwas nicht. ¯_(ツ)_/¯

Kann man in der ScrollView genauso wie bei einer Scrollpane sagen in welche Richtung man die scrollbalken haben möchte?

Was noch sein könnte: Hast du eventuell beim setzen der ContentSize width und height vertauscht?
Nicht das ich dich jetzt blöd da stehen lassen möchte, aber manchmal passieren ja so komische Fehler. Da will ich mich selber nicht ausnehmen :wink:

ne ich hab auch extra zu Testzwecken feste Werte reingesetzt die man sehen müsste
Ich werde nachher mal nen Minibeispiel posten

*** Edit ***
@cresse ne leider nicht, zumindest nicht dass ich wüsste

*** Edit ***


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = true;
    UIScrollView* scrollView = [UIScrollView new];
    [scrollView setShowsHorizontalScrollIndicator:false];
    [scrollView setTranslatesAutoresizingMaskIntoConstraints:false];
    scrollView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:scrollView];
    
    NSLayoutConstraint *myConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5];
    
    [self.view addConstraint:myConstraint];

    myConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-5];
    
    [self.view addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:5];
    
    [self.view addConstraint:myConstraint];
    myConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5];
    
    [self.view addConstraint:myConstraint];
    
    UILabel* infoTextLabel = [UILabel new];
    infoTextLabel.font = [UIFont systemFontOfSize:17.0];
    infoTextLabel.text = @"A suspension can only be as good as its setup…

Unfortunately, there is no perfect setup which works for each and every track. You need to fine-tune your suspension in order to get perfect traction.
But how to keep track of your setup if you do not have your own World Cup mechanic at hand?
MyBikeSetup helps you to find and keep your settings stored for each track. It works for every fork and rear shock and you always have it at hand, right on your mobile phone...";
    infoTextLabel.numberOfLines = 0;
    
    [infoTextLabel setTranslatesAutoresizingMaskIntoConstraints:false];
    [scrollView addSubview:infoTextLabel];
    
    
    UILabel* versionTitleText = [UILabel new];
    versionTitleText.text = @"Version:";
    versionTitleText.font = [UIFont systemFontOfSize:17.0];
    [versionTitleText setTranslatesAutoresizingMaskIntoConstraints:false];
    [scrollView addSubview:versionTitleText];

    
    UILabel* versionText = [UILabel new];
    versionText.text = @"1.2.3.4";
    versionText.font = [UIFont systemFontOfSize:17.0];
    [versionText setTranslatesAutoresizingMaskIntoConstraints:false];
    
    [scrollView addSubview:versionText];

    
    UILabel* copyrightText = [UILabel new];
    copyrightText.text = @"copyright";
    copyrightText.font = [UIFont systemFontOfSize:17.0];
    [copyrightText setTranslatesAutoresizingMaskIntoConstraints:false];
    
    [scrollView addSubview:copyrightText];

    
    UILabel* developerTitleText = [UILabel new];
    developerTitleText.text = @"Developer:";
    developerTitleText.font = [UIFont systemFontOfSize:17.0];
    [developerTitleText setTranslatesAutoresizingMaskIntoConstraints:false];
    
    [scrollView addSubview:developerTitleText];

    
    UILabel* developerText = [UILabel new];
    NSMutableString* text = [NSMutableString new];
    
    developerText.text = @"dev";
    developerText.numberOfLines = 0;
    developerText.font = [UIFont systemFontOfSize:17.0];
    [developerText setTranslatesAutoresizingMaskIntoConstraints:false];

    [scrollView addSubview:developerText];

    
    UILabel* translatorTitleText = [UILabel new];
    translatorTitleText.text = @"Translator:";
    translatorTitleText.font = [UIFont systemFontOfSize:17.0];
    [translatorTitleText setTranslatesAutoresizingMaskIntoConstraints:false];
    
    [scrollView addSubview:translatorTitleText];
    

    bool translators = true;
    UILabel* translatorText = [UILabel new];
    text = [NSMutableString new];
    
    translatorText.text = @"trans";
    translatorText.numberOfLines = 0;
    translatorText.font = [UIFont systemFontOfSize:17.0];
    [translatorText setTranslatesAutoresizingMaskIntoConstraints:false];
    
    [scrollView addSubview:translatorText];

    
    //info label
    myConstraint = [NSLayoutConstraint constraintWithItem:infoTextLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5];
    
    [scrollView addConstraint:myConstraint];
    
    myConstraint = [NSLayoutConstraint constraintWithItem:infoTextLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
    
    [scrollView addConstraint:myConstraint];
    
    myConstraint = [NSLayoutConstraint constraintWithItem:infoTextLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-5];
    
    [scrollView addConstraint:myConstraint];
    
    //version label
    myConstraint = [NSLayoutConstraint constraintWithItem:versionTitleText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5];
    
    [scrollView addConstraint:myConstraint];
    
    myConstraint = [NSLayoutConstraint constraintWithItem:versionTitleText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:infoTextLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    
    [scrollView addConstraint:myConstraint];
    
    //version number label
    myConstraint = [NSLayoutConstraint constraintWithItem:versionText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:versionTitleText attribute:NSLayoutAttributeRight multiplier:1.0 constant:5];
    
    [scrollView addConstraint:myConstraint];
    
    myConstraint = [NSLayoutConstraint constraintWithItem:versionText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:versionTitleText attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    
    [scrollView addConstraint:myConstraint];
     
    //copyright
    myConstraint = [NSLayoutConstraint constraintWithItem:copyrightText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5];
    
    [scrollView addConstraint:myConstraint];
    
    myConstraint = [NSLayoutConstraint constraintWithItem:copyrightText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:versionTitleText attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5];
    
    [scrollView addConstraint:myConstraint];
    
    
    // developer title
    myConstraint = [NSLayoutConstraint constraintWithItem:developerTitleText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5];
    
    [scrollView addConstraint:myConstraint];
    
    myConstraint = [NSLayoutConstraint constraintWithItem:developerTitleText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:copyrightText attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    
    [scrollView addConstraint:myConstraint];

    
    //developers
    myConstraint = [NSLayoutConstraint constraintWithItem:developerText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:developerTitleText attribute:NSLayoutAttributeRight multiplier:1.0 constant:5];
    
    [scrollView addConstraint:myConstraint];
    
    myConstraint = [NSLayoutConstraint constraintWithItem:developerText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:developerTitleText attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    
    [scrollView addConstraint:myConstraint];

    
    
    if(translators)
    {
      // translator title
      myConstraint = [NSLayoutConstraint constraintWithItem:translatorTitleText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5];
    
      [scrollView addConstraint:myConstraint];
    
      myConstraint = [NSLayoutConstraint constraintWithItem:translatorTitleText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:developerText attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    
      [scrollView addConstraint:myConstraint];
    
    
      //translators
      myConstraint = [NSLayoutConstraint constraintWithItem:translatorText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:translatorTitleText attribute:NSLayoutAttributeRight multiplier:1.0 constant:5];
    
      [scrollView addConstraint:myConstraint];
    
      myConstraint = [NSLayoutConstraint constraintWithItem:translatorText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:translatorTitleText attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    
      [scrollView addConstraint:myConstraint];
        
        
        scrollView.contentSize = CGSizeMake(90, 20);
    }
}

ist leider doch etwas länger geworden

wie siehts aus habt ihr ne Idee?

ok der Trick ist, die Constraints müssen auch die Width abdecken und wenn es dann nicht in der Höhe scrollt muss man die Höhen Constraints noch einmal überprüfen.
Dann läuft alles auch ohne ContentSize