2017-06-10 1 views
0

Ich weiß, wie UIButton programmgesteuert zu einem UIView hinzufügen. Aber ich bin beim Hinzufügen der UIButton in der neuen YPosition fest, nachdem eine bestimmte Anzahl von UIButtons in der vorherigen YPosition hinzugefügt wurden.Hinzufügen von mehreren UIButton in UIView in verschiedenen Y-Position

Hier ist mein Fall:

  • Anzahl der UIButtons kommt von NSArray hinzugefügt werden - der 20

  • UIView ist bereits in der UIViewController hinzugefügt nehmen lassen - die


    'buttonView' Lassen Sie rufen
  • Ich erstelle UIButton mit der Schleife (bis zu 20). Ich habe festgelegt, den
    button 70 und Button als 45

  • ich die Bildschirmgröße bekommen und dies durch die buttonWith Teilung, so
    als die Anzahl der Elemente zu berechnen, die pro Zeile untergebracht werden kann oder pro Y-Position.

  • Wenn die Bildschirmbreite 414 ist, dann 414/70 = ~ 5.9 und ich nehme es als

  • First 5 UIButton der YPosition als 0 und XPosition beginnt bei 0 und dem Hinzufügen von button für nachfolgende Tasten.

  • Next 5 UIButtons (6-10) sollte die neue YPosition hat als 1-5 UIButton YPosition (0) + Button (45) + somespace (10) = 55 und XPostion sollte beginnen mit 0.

  • für den nächsten Satz von UIButtons (11-15), sollte die YPosition seinen
    YPosition (55) + Button (45) + somespace (10) = 100 und XPosition beginnt mit 0

Wie erreiche ich das? Unten ist mein Code

xPos = 8.0f; 
yPos = 0.0f; 
totalSlots =timeslots.Count - //Total Number UIButtons to be creted 
buttonWidth = 70; 
buttonHeight = 45; 
buttonViewWidth = this.view.frame.size.width // I'm getting the screen 
buttonPerLine = scrollViewWidth/(buttonWidth + 10); // This calculates no.of.buttons per line/Y Position. I'm rounding off as said before 
numberOfLine = totalSlots/buttonPerLine // This gives how many lines needed 

for (int i = 0; i < totalSlots; i++) 
         { 
timeSlotbtn[i] = new UIButton(); 
         timeSlotbtn[i] = new UIButton(); 
         timeSlotbtn[i].Frame = new CGRect(xPos, yPos, buttonWidth, buttonHeight); 
         timeSlotbtn[i].SetTitle(buttonText, UIControlState.Normal); 
         timeSlotbtn[i].Layer.BorderWidth = 2.0f; 
         timeSlotbtn[i].Layer.CornerRadius = 3; 
         timeSlotbtn[i].Tag = i; 
         timeSlotbtn[i].TitleLabel.Font = UIFont.FromName("HelveticaNeue-bold", 14.0f); 


       xPos = timeSlotbtn[i].Frame.Location.X + buttonWidth + buttonSpace; 
       timeslotScrollView.AddSubview(timeSlotbtn[i]); 
} 

Bitte beachten Sie den obigen Code ist in Xamarin.iOS geschrieben. Wenn Sie die Lösung kennen, können Sie wahrscheinlich in Objective-c schreiben. Wie auch immer, die Logik sollte gleich sein.

+0

Ich löste dies. Ich habe den Code aktualisiert, falls jemand das gleiche sucht. – user3310076

Antwort

0
  int buttonWidth = 65; 
      int buttonSpace = 5; 
      int buttonHeight = 30; 
      int totalSlots = timeslots.Count; 
      nfloat screenSize = firstViewController.screenSize; 
      nfloat buttonPerLine = screenSize/(buttonWidth + buttonSpace); 
      int roundedButtonPerLine = Convert.ToInt32(Math.Floor(buttonPerLine)); 
      nfloat totalLines = totalSlots/buttonPerLine; 
      int roundedTotalLines = Convert.ToInt32(Math.Ceiling(totalLines)); 
      nfloat determineXPosition = (screenSize - (8 + (buttonWidth * roundedButtonPerLine) + (buttonSpace*(roundedButtonPerLine-1))))/2; 
      nfloat xPos; 
      if (roundedTotalLines > 1) 
      { 
       xPos = determineXPosition; 
      } 
      else 
      { 
       xPos = 8.0f; 
      } 

      nfloat yPos = 0.0f; 
      int currentLine = 1; 
      int loopCount = 1; 

      for (int i = 0; i < room.timeslots.Count; i++) 
      { 
       var buttonText = room.timeslots[i].startDate.ConvertDateToStanfordLocalTime();//hour + ":" + minute + "p"; 
       timeSlotbtn[i] = new UIButton(); 
       timeSlotbtn[i].Frame = new CGRect(xPos, yPos, buttonWidth, buttonHeight); 
       timeSlotbtn[i].SetTitle(buttonText, UIControlState.Normal); 
       timeSlotbtn[i].Layer.BorderWidth = 2.0f; 
       timeSlotbtn[i].Layer.CornerRadius = 3; 
       timeSlotbtn[i].Tag = i; 
       timeSlotbtn[i].TitleLabel.Font = UIFont.FromName("HelveticaNeue-bold", 14.0f); 

       xPos = timeSlotbtn[i].Frame.Location.X + buttonWidth + buttonSpace; 
       timeslotScrollView.AddSubview(timeSlotbtn[i]); 


       if (loopCount == roundedButtonPerLine) 
       { 

        if (roundedTotalLines > 1) 
        { 
         xPos = determineXPosition; 
        } 
        else 
        { 
         xPos = 8.0f; 
        } 
        currentLine++; 
        yPos = timeSlotbtn[i].Frame.Location.Y + buttonHeight + buttonSpace; 
        loopCount = 0; 
       } 
       loopCount++; 
      } 
Verwandte Themen