2015-04-17 4 views
-1

У меня есть один UITableView. Я хочу установить для него ограничения программно. Ниже приведены ограничения, которые я хочу установить.Ограничения для UITableView программно в iOS

  1. Ведущий
  2. Скользящий
  3. Топ
  4. Bottom

Это будет действительно очень полезно, если кто-то дает пример образца.

+0

Это довольно стандартная ситуация. Вы читали документацию по автоматической компоновке? –

ответ

0

Попробуйте это,

NSLayoutConstraint *constraintSet = [NSLayoutConstraint constraintWithItem:YourTableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f]; 
[self.view addConstraint:constraint]; 

Теперь добавьте это атрибуты также,

NSLayoutAttributeTop 
NSLayoutAttributeTrailing 
NSLayoutAttributeBottom 

чем добавить

[yourview addConstraint: constraintSet]; 
+0

Я пробовал с этим. Но не работает. – Rakesh

0

В моем коде обычно используют метод, как показано ниже, вы можете настроить для твои нужды.

-(void)addConstraintForView:(UITableView*)tableView toView:(UIView*)parentView 
{ 

NSMutableArray *constraints = [NSMutableArray new]; 
[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView 
                attribute:NSLayoutAttributeTop 
                relatedBy:NSLayoutRelationEqual 
                 toItem:parentView 
                attribute:NSLayoutAttributeTop 
                multiplier:1.0f 
                constant:0.0f]]; 

[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView 
                attribute:NSLayoutAttributeLeading 
                relatedBy:NSLayoutRelationEqual 
                 toItem:parentView 
                attribute:NSLayoutAttributeLeading 
                multiplier:1.0f 
                constant:0.0f]]; 

[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView 
                attribute:NSLayoutAttributeTrailing 
                relatedBy:NSLayoutRelationEqual 
                 toItem:parentView 
                attribute:NSLayoutAttributeTrailing 
                multiplier:1.0f 
                constant:0.0f]]; 

[constraints addObject:[NSLayoutConstraint constraintWithItem:tableView 
                attribute:NSLayoutAttributeBottom 
                relatedBy:NSLayoutRelationEqual 
                 toItem:parentView 
                attribute:NSLayoutAttributeBottom 
                multiplier:1.0f 
                constant:0.0f]]; 

[parentView addConstraints:constraints]; 
} 
+0

Что мне нужно установить для «parentView». Я устанавливаю self.view Установив self.view, он не работает. – Rakesh

+0

Я думаю, что ваш parentView - это «self.view». Когда вы закончите добавлять ограничения, попробуйте обновить макет представления с помощью «[self.view layoutSubviews]»; – Andr3a88