iOS navigationItem.titleView remove margins - ios

I have something like this:
But I need that the view that am setting for the titleView, doesn't has those margins on the left and right side. Something like this:
This is what am actually doing:
#property (nonatomic, strong) XHScrollMenu *scrollMenu; // where XHScrollMenu is a subclass of UIView
- (void)viewdidLoad{
_scrollMenu = [[XHScrollMenu alloc] initWithFrame:self.navigationController.navigationBar.frame];
_scrollMenu.backgroundColor = [UIColor clearColor];
_scrollMenu.delegate = self;
_scrollMenu.selectedIndex = 0;
self.navigationItem.titleView =self.scrollMenu;
}
I tried giving the view a with of 320, but I got the same result. I read in other post, that maybe a subclass make the trick, but don't know how to implement that solution...
How can I make the title view use the entire width?

Try with bounds instead of frame. see different between bounds and frame.
...initWithFrame:self.navigationController.navigationBar.bounds] /// or give frame manually such like CGRectMake(x,y,self.view.frame.size.width, yourHeight);
If this suggestion doesn't help you then sure problem under initWithFrame of XHScrollMenu. Go in this method and check there.

Related

iOS8 issue - UIView drawRect is not called

I have a very simple UIView, that is only drawing a triangle. It implements a UIView drawRect method that is drawing a figure. It is working fine on iOS7, but when I run the same code on iOS8 it is not even called. Any ideas what has changed on iOS8? My code in nutshell:
#interface MyView : UIView
#end
#implementation MyView
- (instancetype)init {
self = [super init];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
// draw things
}
#end
myView = [MyView new];
[self addSubview:myView];
Update
My view hierarchy: UIViewController View->Custom UILabel->MyView
It is an error message bubble, and my view is a beak pointing to something.
I have used a new UI debugging tool, but it is not visible there. Init method is called, drawRect is not. Something must have changed in iOS8, because iOS7 is calling drawRect.
Update 2
Of course I'm using constraints (and Masonry pod), and this is why I did not specify the frame.
[myView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(make.superview.mas_bottom);
make.centerX.equalTo(make.superview);
make.width.equalTo(#10.0f);
make.height.equalTo(#5.0f);
}];
I have also tried adding [myView setNeedsDisplay] in various places, but it didn't help.
Problem finally solved. I'm not sure what exactly caused the issue (Masonry [constraints framework] or iOS8 changes to UILabel), but the solution was to change the view hierarchy. I created another UIView that contains both UILabel and my UIView (drawn beak) instead of adding the UIView to UILabel as subview. Now drawRect method is called both on iOS7 and iOS8.
Previous hierarchy:
UIViewController View->Custom UILabel->MyView
New hierarchy:
UIViewController View->Container UIView->Custom UILabel & MyView
To sum up, if your drawRect method is not called on iOS8, and you are adding some UIView to UILabel as subview, try to use some container which will contain both UIView and UILabel.
make sure that myView's superview property clipToBounds = NO, and that myView rect is on screen
There is an important detail missing in your approach. You need to specify a frame that determines the origin and size of your view. This can be done with the initWithRect method of UIView, or you can set the frame after allocation/initialization. Since you have a custom init method already I would do the following:
myView = [MyView new];
myView.frame = CGRectMake(0.0,0.0,100.0,100.0);
[self addSubview:myView];
This will draw your custom view with an origin point of (0.0,0.0) and a width and height of 100.0.
Furthermore, adding a call to setNeedsDisplay should force the drawRect method to be called if you are still having trouble:
[myView setNeedsDisplay];

Xcode - Image view not hiding

I have an image view in a basic app that I am attempting to set to hidden on load using:
- (void)viewDidLoad
{
my_image.hidden = YES;
}
This code along with some other attribute changes are not functioning at all. I have synthesized the my_image property. Any ideas why this may not be working? Also, please let me know if you need any further information. I'm new to this and it's really bugging me, so thanks in advance!
It's hard to make it clear with a simple code line, my_image.hidden = YES;
But I think you can do the things below :
Print the imageView (your 'my_image' object) in the console to look into it.
Use the other properties of the imageView to see if you can manipulate it, such as changing the frame, setting the background color, or setting an image for it. If you can change the frame, then you may set my_image.hidden = NO; somewhere else.
Create another UIImageView object and add try it!
If none of above works, you can set the frame of 'my_image' to CGRectZero to hide it.
Maybe you forgot to connect the IBOutlet ? Are you using .xib ?
make a breakpoint at line of my_image.hidden = YES; Does it go into the breakpoint ? If it goes into , make sure the my_image is not nil.
UIImageView *imageview = [[UIImageView alloc]init];
[imageview setHidden:YES];
Setter worked for me:
UIImageView * bb = (UIImageView*)[self.view viewWithTag:1];
[bb setHidden:YES];
I had the same problem with hiding the image view but if you remove the #property statement the hidden behaves as it it should.
Set the hidden in viewdidLoad then the IBaction - works fine
.h
IBOutlet UIImageView *crackedimage1;
.m
-(void)viewdidLoad
crackedimage1.hidden = YES;
- (IBAction)crackaction1:(id)sender {
crackedimage1.hidden = NO;};

Text in UITextView not display correctly, invisible, but occupied spaces

There is a UITextView in the view, the view controller is showing with Modal style. In my viewDidLoad method, I set the text of this UITextView, but, the text is not showing. Image below showing the error. Text color is black.
The weird thing is , when I long tap in the text view or tap [return] in keyboard, the text become visible. One thing I noticed is this error only occurred when the text I set is longer than the UITextView frame width, and the last word is not broken such as a long url.
I think the problem is maybe the word wrap not work correctly.
Thanks in advance.
Code like below:
UITextView *myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 520, 220)];
myTextView.textColor = [UIColor blackColor];
myTextView.font = [UIFont systemFontOfSize:20];
myTextView.layer.cornerRadius = 5;
myTextView.delegate = self;
myTextView.text = #"long text should some like http://stackoverflow.com/posts/11200726/edit";
[self.view addSubview:myTextView];
[myTextView release];
RESOVLED. In viewDidLoad method, add code below:
CGRect tempFrame = myTextView.frame;
[myTextView setFrame:CGRectZero];
[myTextView setFrame:tempFrame];
Subclass UITextView, to a class of your own, let's say MyUITextView:UITextView.
Initialize it offscreen with
[[MyUITextView alloc] initWithFrame:CGRectZero]
In MyUITextView override the method
-(void)willMoveToWindow:(UIWindow *)newWindow
and in it, set self.frame to the proper value.
this worked for me!
Are you sure the text color is black? Also make sure that the image is behind the textView in the view hierarchy. TextView handles Word wrap automatically.
I think in this case, the viewDidLoad is getting called before modelViewController is presented.
Set the text after modalViewController is presented.
If that also not working, then after presenting model, call a method to set the text explicitly.
Check your view hierarchy. You might have added another textView above that view.
Try changing the background color of textView. It must appear is UITextView is added there. And make sure you are not adding another view as a subview above your UITextView. If the textColor is black it must be visible, wordwrap is done by default automatically unless you change the property.Also try to check the attributes settings in XIB if added and plz show the code if adding programmatically.
I think problem in:
self.view
Try add this code line at top
self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
And if not will help, try remove this code line
myTextView.layer.cornerRadius = 5;

iOS: Can't show view after hiding it

I have a activityIndicator and a transparent black layer behind it. When something loads I show them like this:
[activityIndicator startAnimating];
loadingCover.hidden = NO;
And when I hiden them, I just do it like this:
[activityIndicator stopAnimating]; //hides on stop
loadingCover.hidden = YES;
So far everything works. But somehow if I want to show them again, it doesn't work. Any ideas?
EDIT:
This is how I do it...
- (void)viewDidLoad {
[mainView addSubview:loadingCover]; //works
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[loadingCover removeFromSuperview]; //works
}
- (void)refreshRSS:(id)sender {
[mainView addSubview:loadingCover]; //doesn't work
}
EDIT2:
First of all I'm coding with ARC mode and second, loadingCover has been changed to loadingplate, no biggie...
Ok so in my .h file I do this:
UIView *loadingplate;
UIActivityIndicatorView *loadingIndicator;
And in my .m file in viewDidLoad I do this:
CGRect viewRect = CGRectMake(0, 0, 320, 460);
loadingplate = [[UIView alloc] initWithFrame:viewRect];
UIColor *loadingplateColor = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
[loadingplate setBackgroundColor:loadingplateColor];
loadingplate.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;
loadingIndicator = [[UIActivityIndicatorView alloc] initWithFrame:viewRect];
[loadingIndicator setContentMode:UIViewContentModeCenter];
[loadingIndicator startAnimating];
loadingIndicator.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;
[loadingplate addSubview:loadingIndicator];
[mainView addSubview:loadingplate];
EDI3:
After reading Jasarien's answer I tried this in my .h file:
#property (strong, nonatomic) UIView *loadingPlate;
And then in my .m file:
#synthesize loadingPlate = _loadingPlate;
In this file in viewDidLoad I'm creating the view.
But this doesn't seem to work either.
Please see XJones' comment. This answer is probably wrong.
After Edited question:
It seems that since you're using arc, your loadingPlate view is probably being released too soon.
When you alloc and init your loadingPlate view, it'll have a retain count of +1, as expected from an alloc/init. Yet, because you're using ARC, after you add it as a subview of your main view, ARC releases it. Effectively the loadingPlate view is owned by its super view and when you remove it from the superview, the superview releases it, causing the view to be deallocated. Thus when you try to show it again, it's not there anymore and can't be shown.
The solution to this would be to create a property for your loadingPlate view, and give it a 'strong' reference, (effectively the same as declaring the property as a 'retain'). This way, the object that has this property will own the loadingPlate view, keeping a strong reference to it.
You're saying loadingCover.hidden = NO does not work? Have you added the view as a subview to the current showing view?
Try moving the allocation and initialization of that view to the init method. Also, when you call the method to add it again, one thing you can try is to simply check if it exists first probably by:
if (!loadingPlate) {
// allocate and initialize again because somehow it has been released.
}
At least this way you know if the instance of loadingPlate is still being retained.
Your basic logic to create, show, and hide loadingPlate looks fine. I use a very similar implementation abstracted in a UIView subclass in my apps. All of the answers are stabs in the dark b/c the code you posted doesn't show an obvious problem other than the discrepancies in your iVar names. I decided to add this answer in the hope that it helps you find the solution.
You don't need a property. As far as retain semantics go, iVars are strong by default. If you do create a property and synthesize it the way you show than be sure to refer to either self.loadingPlate or _loadingPlate in your code.
Make sure you don't have a typo in your iVar name vs your property name. For example, if you have an iVar called loadingplate but your property is loadingPlate then you will end up with two iVars (loadingplate and loadingPlate) as properties automatically create iVars if they don't match one already defined.
Your logic of creating the loadingPlate view in viewDidLoad and adding/removing it from the superview as needed is totally fine. Just make aure you don't have loadingPlate = nil anywhere other than in viewDidUnload.
If it's possible for the superview frame size to change you should explicitly set loadingPlate.frame = mainView.bounds before adding loadingView as a subview. If the frames don't change this won't matter.
If I think of anything else I'll add it later. Good luck.
Are you sure loadingCover doesn't still have its hidden property set to YES from the previous time displayed?
Try:
- (void)refreshRSS:(id)sender {
loadingCover.hidden = NO;
[mainView addSubview:loadingCover]; //doesn't work
}
Don't you have to use self.loadingplate to call the property strong and make the retain on your view ?

UIScrollView won't show in UIView

My app is building purely programmatically on UITabBarController above a UINavigationController, which are both declared in my AppDelegate. Inside my navigationcontroller, I'm showing a UIViewController, a custom class. This custom class should show a custom UIView in a UIScrollView and a UIPageControl.
Heres my problem:
self.view seems to create an EXC_BAD_ACCESS error when I call it without declaring self.view = [[UIView alloc] init] (or similar). I was wondering if this was a problem with -(void) loadView but seems like it produces the same error in -(void)viewDidLoad. So I basically had to use self.view = scrollView to even show my scrollView, considering [self.view addSubview:scrollView] produced an error. My UIPageControl should stay on the page all the time, and actually be another part of the view than the UIScrollView. So I tried to add a container-view like this
Code:
container = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
// EDIT: ofcourse, I'm also resizing the frame with [scrollView setContentSize:] later, but this is farfetched code to add here.
[container addSubview:scrollView];
self.view = container;
Unfortunately, it seems that I don't get any result at all, and what appears is just an empty view. However, if I add a UILabel or similar, it shows:
[container addSubview:[[UILabel alloc] initWithFrame:CGRectMake(50,50,50,50)]]; // entered above or beneath the addSubview:scrollView line.
My question is: Why doesn't my scrollView appear in the UIView container?
I've noticed that some tutorials say that scrollView must have a delegate, and I agree with the logic - however I can't seem to find out how I set that delegate when I am in my CustomUIViewController-class instead of my AppDelegate.
after you change the UIScrollView size you should use:
[scrollView setNeedsDisplay:YES];
also you implement Delegates the same way you do in other classes:
.h:
#interface MyClass : NSObject <UIScrollViewDelegate>
Okay, the problem seemed to be the initialization - I didn't realize that frame and content was two different things. Seems like the frame that is initializing the view should be whatever size the view should fill, while content is the actual content of whatever should be scrolled. So when I was having problems with user interaction, it was really this.
The problem of why it didn't show in the first place was (stupid.) that the frame was initially, and never changed from, 0,0 so I really lied in my first post.
Thanks to UIScrollView and PageControl: space between views who solved my problem with user interaction.
My steps was to backtrace from self.view:
NSLog(#"%f\n%f",
((UIScrollView*) [[self.view subviews] objectAtIndex:0]).frame.size.width,
((UIScrollView*) [[self.view subviews] objectAtIndex:0]).frame.size.height);
when I realized these were 0 and 0, fixing the problem wasn't too hard :) thanks though, for your efforts Kristian.

Resources