2015-06-04 5 views
1

Привет, я хотел бы спросить, как создавать множественные представления программно и показывать их на экране, я пробовал это, но что-то не хватает.Создание множественных представлений программным способом

int x,y; 

x= 0; 
y=50; 

for (int i=0; i<4; i++) 
{ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 300, 100)]; 

view.backgroundColor = [UIColor redColor]; 


[self.view addSubview: view]; 

    y+=40; 
} 
+0

какой метод у вас есть этот код? –

+0

какой метод вы используете? Я пытаюсь сделать несколько UIViews в swift, но я думал, что сделать это так, чтобы переопределить то же самое и удалить предыдущий из супервизора? – TheeBen

ответ

0
int x,y; 
x= 0; 
y=50; 

for (int i=0; i<4; i++) 
{ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 300, 100)]; 

    view.backgroundColor = [UIColor redColor]; 
    view.layer.cornerRadius = 5.0f; 
    [self.view addSubview: view]; 
    y+=140; //y for next view should be height of previous view and margin between view 
} 
+0

Спасибо, как сделать их округлой формой прямоугольника? –

+0

Попробуйте установить 'view.layer.cornerRadius' – Sujay

+0

Здравствуйте, я хотел бы спросить вас, как я могу обновить размер кадра при перетаскивании его. Я пробовал этот код: { UITouch * touch = [касается любого объекта]; CGPoint thisTouch = [touch locationInView: [touch view] .superview]; CGPoint prevTouch = [touch previousLocationInView: [touch view] .superview]; CGFloat dx = thisTouch.x - prevTouch.x; если ([touch view] .frame.origin.x + dx <-500) { [touch view] .frame = CGRectMake ([touch view] .frame.origin.x + dx, [touch view]. frame.origin.y, [touch view] .frame.size.width, [touch view] .frame.size.height); } –

0
int x,y; 

    x= 0; 
    y=50 
    for (int i=0; i<4; i++) 
    { 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 30, 30)]; 

    view.backgroundColor = [UIColor redColor]; 
    y+=40; 

    [self.view addSubview: view];  
    } 
2
int x,y,paddingX,widthOfView,HeightOfView; 

x= 0; 
y=50; 
paddingX = 10; 
widthOfView = 100; 
HeightOfView = 100; 

for (int i=0; i<4; i++){ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, widthOfView, HeightOfView)]; 
    view.backgroundColor = [UIColor redColor]; 
    [self.view addSubview: view]; 

    y+= widthOfView + paddingX; 
} 

После кода может помочь вам.

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