Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set UICollectionView right align?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.03k
    Comment on it

    Hi Readers,

    This blog will display the collection view content in Right To Left language such as Hebrew/ Arabic. Generally, you will see the collection view as displayed in the below screenshot.

    To set UICollectionView right align first make a property of UICollectionView
    example ->

    @property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

     

    After this add following code to "viewDidLoad"

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.myCollectionView setTransform:CGAffineTransformMakeScale(-1, 1)];
    
        // Do any additional setup after loading the view.
    }
    

     

    Now you will see mirror image of your collection view along with right align cell , like in below screenshot.

     

    Now to remove this strange mirror effect,don’t forget to add below line to add in “cellForItemAtIndexPath”.

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        DemoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DemoCollectionViewCell" forIndexPath:indexPath];
        [cell.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
        NSString *str = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        NSString *mainStr = @"Label ";
        mainStr = [mainStr stringByAppendingString:str];
        [cell.labelTxt setText:mainStr];
        return cell;
    }

     

    Now run your app, and you will see output like following screenshot.

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: