2014-01-27 7 views
1

iOS новичок здесь.Как сгруппировать элементы пользовательского интерфейса в UIView?

У меня есть три элемента пользовательского интерфейса (один UIImageView и два UILabel ы), которые добавляются к UIView (, который заполняет весь экран)

Как центрировать их к середине экрана, как по вертикали, и горизонтально?

+0

Вы можете делать то, что хотите сделать с AutoLayout или с помощью распорок и пружин, но метод отличается в зависимости от того, что вы используете. Что он? –

ответ

2

В коде, без autolayout, вы могли бы использовать autoresizingmasks #oldschool:

imageview.center = view.center; 
label.center = view.center; 
imageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 
2

Используя подробный синтаксис AutoLayout, вы можете установить центр представлен вид в X и Y координаты.

[containerView addConstraint:[NSLayoutConstraint constraintWithItem:otherView 
                  attribute:NSLayoutAttributeCenterX 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:containerView 
                  attribute:NSLayoutAttributeCenterX 
                 multiplier:1 
                  constant:0]]; 

[containerView addConstraint:[NSLayoutConstraint constraintWithItem:otherView 
                  attribute:NSLayoutAttributeCenterY 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:containerView 
                  attribute:NSLayoutAttributeCenterY 
                 multiplier:1 
                  constant:0]]; 

Вы также можете использовать XIB для этого.

+0

Кажется, что не работает ... не правда ли, потому что я использую [iCarousel] (https://github.com/nicklockwood/iCarousel)? –

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