I'm struggling with a strange issue... I've set everything up using Storyboard.
Depending on the size of text contents an uitextview has, I have to programmatically resize & reposition it. However, it seems like it's not repositioning as I wish :(
-(void)viewWillAppear:(BOOL)animated
{
//ansContent is connected to the uitextview via IBOutlet
ansContent.frame = CGRectMake(27, 90, 270, 10000);
ansContent.text = #"very very long text here";
}
It seems like the changing the text works... not the frame :S What is wrong with it?
All I did in Storyboard was to drag & drop an uitextview and connect it to ansContent.
Disable the auto layout option in storyboard. In your case, the autolayout is preventing it from displaying it in proper location. Once you have disabled it, you can set the frame programmatically.
Related
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
Using Xcode5 and auto layout. Consider following scenario:
I have an outlet for "Dispatch" UITextField
#property (weak, nonatomic) IBOutlet UITextField *dispatchTextField;
If user clicks "Camera" button I want to hide dispatchTextField and move "Subject" and "Body"(below) up.
This is not a real scenario but I'm facing tasks where I will be using this kind of technique. I've seen code samples where container size can be modified and so on. In XAML - there is "StackPanel", in Android there is similar controls where I can just hide this TextField and views below automatically spring up.
So, what is the proper way to do this in XCode5 with auto-layout?
I tried (with no luck)
self.recipientTextField.hidden = YES;
I also tried
[self.recipientTextField setFrame:CGRectMake(0, 0, 0, 0)];
[self.view layoutIfNeeded];
Add a height constraint to the text field. After making sure there are no conflicting vertical constraints, add a reference to this constraint in your view controller.
Add an action to the Camera button and connect it to the view controller. Whenever the button is tapped, you would set the constant property of the height constraint to zero.
Make sure that other views are setup to have constant vertical spacing with the text field you wish to shrink.
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.
Hey Im new to IOS development, here is the situation, I have UIView displaying a long text with UILable, actually the text is somehow long and the exceed the screen size, but the UIView don't have a scrollbar or other control to drag down to view the text, is there any ways to do that?
Thanks!
You should use UITextView instead of UILabel. You can disable UITextView for editing like this.
txtview.editable = NO;
UITextView will automatically add scroll if the text is long then UITextView frame.
Hope this helps.
Here is what you need to do, decrease the height of the UIView in which the UILabel is , also use UITextView instead of UILabel.
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.