Sidharth J Dev
Sidharth J Dev

Reputation: 987

Change Position Of a UIView programatically

ScreenShot

There is an Arabic Page in my app and The "Date Field" in the Arabic Page must align to the right side of the view(right now, it is to the left). I have tried the following code

    -(void)changeDatePosition{
    if (!_isEnglish) {
        CGRect currentDatePostion=_viewForDate.frame;
        [_viewForDate removeFromSuperview];
        currentDatePostion.origin.x+=currentDatePostion.size.width;
        _viewForDate.frame=currentDatePostion;
        [_baseView addSubview:_viewForDate];
    }
}

But that did not help. The view positioned itself to the top of the navigation bar(no change in the x co-ordinate,either). I have used autolayout to align the left leading space of "ViewForDate"(the date field) to the leading space of "PlacesView"(The View on Top of the Date Field). But I set its priority to 250. HOw can I solve this problem?

Upvotes: 1

Views: 607

Answers (4)

Anand V
Anand V

Reputation: 423

Hook an IBOutlet to the leading constraint of the date view and set its value to 0. And then try this code:

-(void)changeDatePosition{
    if (!_isEnglish) {
        _dateViewLeadConstraint.constant = x;
    }
    //x - amount by which view should move right, you can calculate based on your view setup
}

Upvotes: 1

Sidharth J Dev
Sidharth J Dev

Reputation: 987

I have found a solution to the problem and quite happy about it.

The DateView has a Left Alignment(Leading Space) to Places That is set to 0. I made a right alignment for it too(at 144). Then I created Outlets for them namely, leftAlignment(=0) and rightAlignment(=144) and wrote this method.

-(void)changeDateLocation{
    if (!_isEnglish) {
        _leftAlignment.priority=UILayoutPriorityDefaultLow;
        _rightAlignment.constant=0;
        _rightAlignment.priority=UILayoutPriorityDefaultHigh;
    }
    else{
        _leftAlignment.priority=UILayoutPriorityDefaultHigh;
        _rightAlignment.priority=UILayoutPriorityDefaultLow;
        _leftAlignment.constant=0;
    }
}

And this gave me my solution. I thank all you people who have contributed. This method is quite useful I think. I am trying this for the first time

Upvotes: 0

Skywalker
Skywalker

Reputation: 1586

Try this code.

currentDatePosition = CGRectMake(self.view.frame.size.width - currentDatePosition.size.width, currentDatePosition.frame.origin.y, currentDatePosition.frame.size.width, currentDatePosition.frame.size.height);

Upvotes: 1

Jagveer Singh
Jagveer Singh

Reputation: 2318

-(void)changeDatePosition{
    if (!_isEnglish) {
        CGRect currentDatePostion=_viewForDate.frame;
        [_viewForDate removeFromSuperview];
        float ScreenWidth=_viewForDate.frame.size.width;
        currentDatePostion.origin.x=ScreenWidth-currentDatePostion.size.width;
        _viewForDate.frame=currentDatePostion;
        [_baseView addSubview:_viewForDate];
    }
}

can you try it please.May be it solve your problem

Upvotes: 1

Related Questions