I'm trying to update the position and size of several UIButtons in my app by using:
-(void)viewDidAppear:(BOOL)animated {
_UIButton1.frame = CGRectMake(-7, 35, 212, 77);
_UIButton2.frame = CGRectMake(139, 149, 290, 77);
// And so on for several more buttons
}
However, when I use this code, these buttons just appear at wherever the location is set on the Storyboard, not where I'm setting it programmatically. How would I make these buttons appear at a location this is different than the one I have set in my Storyboard?
Open your storyboard file and make sure the right pane is showing the Utilities menu, then uncheck Use Auto Layout.
When Auto Layout is enabled, you have to set constraints on your views and create outlets to those constraints to move things around with code. If you want to work directly with frames, Auto Layout needs to be turned off. It has to be one or the other.
Auto Layout can be great, but it's hard to make a recommendation without knowing more about what you are trying to achieve.
Please update frame in -(void) viewWillAppear method as in viewDidAppear your view is practically set and is ready to displayed.
after updating your button's frame please use following line of code
[self.view laoutIfNeeded];
Thanks
Related
Currently I have multiple UIButtons that have an image on the left side (added through the IB). I'd like to add a UIImageView subview to the buttons programatically to the right side. Currently I am doing it in this manner:
UIImageView *rightArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menu-arrow.png"]];
rightArrow.frame = CGRectMake(button.frame.size.width - 32, button.frame.size.height/2 - 9, 17, 17);
rightArrow.tag = 22;
[button addSubview:rightArrow];
This works great in adding the button and placing it on the right side. However, doing this in the ViewDidLoad method creates issues. In the IB the width of the button is the width of the view. So in the ViewDidLoad method the button's width hasn't been changed by autolayout. So it doesn't show up on the far right side.
So I moved it to the ViewDidLayoutSubviews. But on iOS 8, this is called multiple times! And on iOS 7 it's called once! Also, in iOS 8 the LAST call by ViewDidLayoutSubviews is the most accurate in terms of placing the image!
How can I solve this particular issue? Also I'd like to do this programatically rather than try and mess with the IB if possible.
Edit: Turns out the best answer (IMO) was from Mr. T (in the comments) which stats putting the code in ViewDidAppear and then doing a check if you've already added the ImageViewto the button.
But on iOS 8, this is called multiple times! And on iOS 7 it's called once! Also, in iOS 8 the LAST call by ViewDidLayoutSubviews is the most accurate in terms of placing the image!
So respond each time viewDidLayoutSubviews is called. If the image view is already added, don't add it again — but do reposition it if necessary.
An even better approach, however, would be be to position the image view itself using autolayout! Instead of assigning it a frame, give it constraints. Now it will be repositioned automatically in response to view layout, for the rest of time.
I have 2 UITableView in my UIViewController. one of them is in the right side of the screen till center of screen. The other is from center till left bound.
User can hide the right UITableview by pressing one a button on the navigation bar.
I use this code to make left UITableview full screen:
self.rightTableView.hidden= YES;
self.leftTable.frame= CGRectMake(0,
self.dataTableView.frame.origin.y,
self.view.frame.size.width,
self.dataTableView.frame.size.height);
But it is not working.
I even put this code on the viewdidload and viewDidAppear methods but the frame not changing.
Try self.leftTable.translatesAutoresizingMaskIntoConstraints = YES, then call to frame resize at runtime is automatically translated into new constraints.
But, translatesAutoresizingMaskIntoConstraints will also cause new constraints to be added based on the view's autoresizingMask. So make sure other constraints are working properly.
When using autolayout, modifying the frame will have no effect.
Instead, you should modify the constraints.
You can do this by creating outlets for your constraints and connecting then in the interface builder.
I really tried hard to solve this apparently simple problem by myself, but I couldn't fix it yet!
So here's the deal:
I am using a storyboard with auto layout enabled.
But because of to many objects in one of my views, I decided to implement the
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
duration:(NSTimeInterval)duration
method for this view controller and set the frames for the views explicit like so:
self.startStopBtn.frame = CGRectMake(10, 223.5, 300, 30);
And it works fine as long as I keep the size of this UIButton object the same (as in the storyboard) and only change its origin. As soon as I change the size the button is gone!
Finally enough I can change the frames of the labels, views and image views without any problems in the same method in the exact same way.
I really would appreciate your help!
Hopefully somebody can guide me in the right direction.
if you are using autolayout you should not manimulate UIView frame. You should modify NSLayoutConstaints to change something in view from code.
I have a problem with the frame-property in iOS 7.
I wanna resize some UIViews in the viewDidLoad-method of my UIViewController, but if I do it like int screenHeight = [[UIScreen mainScreen] bounds].size.height;
[self.leftSideTableView setFrame: CGRectMake(0, 0, 320, screenHeight)];
the height is set as I want it till the end of the method, but in every other method it is as is has been before!
What's wrong with it or is it just a bug of the compiler or anything else?
One has to put view resizing into -viewDidLayoutSubviews:! (documentation)
Placing view frame changes into -viewWillAppear: or -viewDidLoad: will not work, because the views are not laying out yet!
Check if you are using autolayout in your xib file. If you don't want to use autolayout, uncheck it in your xib file.
Change your self.leftSideTableView frame in -viewWillAppear:.
Check to make sure that auto layout isn't activated in your storyboard file.
To turn it off, look at the inspector in interface builder. Click the icon that looks like a page all the way on the left. In the section "Interface Builder Document" uncheck "Use Auto Layout."
I find it's best to an entire view controller in IB with auto layout, or completely in code. Mixing the two can lead to weird behavior that is hard to debug.
There are several reasons why this might be happening. First of all, you need to make sure that your tableview isn't nil. If you're creating it programmatically, you need to be sure that you're calling alloc/init somewhere before you attempt to set the frame. If self.leftSideTableView is an IBOutlet, this can be caused by forgetting to actually link the outlet to the interface object.
Then, second and less likely, you are creating the table view programmatically and initializing it properly, but you forgot to add it as a subview of one of your on screen views.
I'm pretty new to ios. I'm working on a project which is going to frequently use a certain UIView class throughout the application. This class is simply an image that does some transparency stuff with the background color. This works fine so far. This class sometimes exists as a lone UIView, and sometimes as a button subview.
When I add this UIView to a UIButton as a subview, the full contents of the UIView are displayed at full size, but the clickable area of the button remains the size defined in the xib unless Use Autolayout is turned off (even if I manually try to set the button's frame.)
I would like to put a UIButton on the xib as a placeholder, and then later define its size/clickable area based on the size of the overlay image which the UIView that was initialized with.
Should this be possible, or am I misinterpreting the usage of xib files/autolayout?
under ViewDidLoad I have...
- (void)viewDidLoad{
[theButton addSubview:_theView];
CGRect buttonFrame = theButton.frame;
buttonFrame.size = CGSizeMake(_theView.getSize.width,_theView.getSize.height);
[theButton setFrame:buttonFrame];
}
However, the frame stays the same size when I print the button info before and after I try calling setFrame.
(note that '_theView.getSize' was added by me)
Under Autolayout, views don't have frames at viewDidLoad. Try your code in viewWillAppear: or viewDidLayoutSubviews.
Under Autolayout, you don't set frames. You edit constraints instead. Setting a frame will work until the next layout pass, when your layout will revert to that described by your constraints.
To size a button to fit a subview, you can try something like this (in visual format language): |-[theView]-| but it would depend what constraints are in place from your xib.